• python os.path模块


    1、os.path.realpath(path)  #返回path的真实路径

    2、os.path.split(path)  #把路径分割成dirname和basename,返回一个元组

    例子:

    print(__file__)          // 1.py
    print(os.path.realpath(__file__))  // /root/python/1.py
    print(os.path.split(os.path.realpath(__file__)))  // ('/root/python','1.py')
    print((os.path.split(os.path.realpath(__file__)))[0])  // /root/python
    

    3、os.path.abspath(path) #返回绝对路径

    例子:

    print(os.path.abspath('1.txt'))    // /root/python/1.txt
    print(os.path.abspath(__file__))    // /root/python/1.txt

    4、os.path.basename(path) #返回文件名

    5、os.path.dirname(path) #返回文件路径

    例子:

    print(os.path.basename('/root/python/1.txt'))    // 1.txt
    print(os.path.dirname('/root/python/1.txt'))  // /root/python

    6、os.path.exists(path)  #路径存在则返回True,路径损坏返回False

    7、os.path.isabs(path)  #判断是否为绝对路径

    8、os.path.isfile(path)  #判断路径是否为文件

    9、os.path.isdir(path)  #判断路径是否为目录

    10、os.path.islink(path)  #判断路径是否为链接

    例子:

    print(os.path.exists('/root/python'))      // True
    print(os.path.exists('/root/python/1.txt'))  // True
    print(os.path.exists('/root/python2'))     // False
    print(os.path.isabs('../python/1.txt'))       // False 
    print(os.path.isabs('/root/python/1.txt'))    // True
    print(os.path.isfile('/root/python/1.txt'))   // True
    print(os.path.isfile('/root/python/'))       // False
    print(os.path.isdir('/root/python/1.txt'))    // False
    print(os.path.isdir('/root/python'))        // True
    print(os.path.islink('/root/python/1.txt'))   // False,如果是软连接则返回True

    11、os.path.getatime(path)  #返回最后一次进入此path的时间。

    12、os.path.getmtime(path)  #返回在此path下最后一次修改的时间。

    13、os.path.getctime(path)  #返回path的创建时间

    14、os.path.getsize(path)  #返回文件大小,如果文件不存在就返回错误

    例子:

    print(os.path.getatime('/root/python/1.txt'))    // 1550213633.63
    print(os.path.getmtime('/root/python/1.txt'))   // 1550213527.49
    print(os.path.getctime('/root/python/1.txt'))   // 1550213527.49
    print(os.path.getsize('/root/python/1.txt'))    // 16

    15、os.path.join(path1[, path2[, ...]])  #把目录和文件名合成一个路径

    例子:

    path = "/var/www"
    print(os.path.join(path,"python"))    // /var/www/python
    

    16、os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。

    17、os.path.lexists  #路径存在则返回True,路径损坏也返回True

    18、os.path.expanduser(path)  #把path中包含的"~"和"~user"转换成用户目录

    19、os.path.expandvars(path)  #根据环境变量的值替换path中包含的”$name”和”${name}”

    20、os.path.ismount(path)  #判断路径是否为挂载点()

    21、os.path.normcase(path)  #转换path的大小写和斜杠

    22、os.path.normpath(path)  #规范path字符串形式

    23、os.path.relpath(path[, start])  #从start开始计算相对路径

    24、os.path.samefile(path1, path2)  #判断目录或文件是否相同

    25、os.path.sameopenfile(fp1, fp2)  #判断fp1和fp2是否指向同一文件

    26、os.path.samestat(stat1, stat2)  #判断stat tuple stat1和stat2是否指向同一个文件

    27、os.path.splitdrive(path)   #一般用在windows下,返回驱动器名和路径组成的元组

    28、os.path.splitext(path)  #分割路径,返回路径名和文件扩展名的元组

    例子:

    print(os.path.splitext('/root/python/1.txt'))    // ('/root/python/1',‘.txt’)

    29、os.path.splitunc(path)  #把路径分割为加载点与文件

    30、os.path.walk(path, visit, arg)  #遍历path,进入每个目录都调用visit函数,visit函数必须有3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有文件名,args则为walk的第三个参数

    31、os.path.supports_unicode_filenames  #设置是否支持unicode路径名

  • 相关阅读:
    Beta冲刺(4/4)
    2019 SDN上机第7次作业
    Beta冲刺(3/4)
    Beta冲刺(2/4)
    机器学习第二次作业
    机器学习第一次作业
    软工实践个人总结
    第04组 Beta版本演示
    第04组 Beta冲刺(5/5)
    第04组 Beta冲刺(4/5)
  • 原文地址:https://www.cnblogs.com/eline2018/p/10383777.html
Copyright © 2020-2023  润新知