zoukankan      html  css  js  c++  java
  • Python 学习记录1

    怎样得到文件的扩展名?

    >>> filepath = r'c:\test\test2\hello.txt'
    >>> import os
    >>> os.path.splitext(filepath)
    ('c:\\test\\test2\\hello', '.txt')


    怎样解析 url ?

    >>> url = 'http://www.test.com.cn/news/test.asp?a=3&b=4#hello'
    >>> import urlparse
    >>> urlparse.urlparse(url)
    ('http', 'www.test.com.cn', '/news/test.asp', '', 'a=3&b=4', 'hello')
    >>>


    如何获取路径?

    >>> os.path.dirname('www.sina.com.cn/news/test.asp')
    'www.sina.com.cn/news'
    >>> os.path.dirname(r'c:\test\test2\hello.txt')
    'c:\\test\\test2'


    如何创建和删除目录?

    >>> import os
    >>> path = r'c:\test2'
    >>> os.makedirs(path)
    >>> os.path.exists(path)
    True
    >>> os.removedirs(path)
    >>> os.path.exists(path)
    False
    >>>

    os.unlink 用于删除文件。 


    os.path.exists() 和 os.path.isdir() 有什么区别?

    isdir() 用于判断一个目录是否存在;
    exists() 可以判断目录或者文件是否存在。

  • 相关阅读:
    运动运行。
    stratMove方法
    抛物线
    表单的小例子吖
    常用的查询DOM的方法
    liuyan
    防止xss攻击。
    ES6
    Map的使用
    ZOJ 3998(线段树)
  • 原文地址:https://www.cnblogs.com/RChen/p/369780.html
Copyright © 2011-2022 走看看