Python模块(包)

Python模块(包)-os.path详解

import os
__file__是指本文件路径
加入本文件的绝对路径是/www/wwwroot/test.py
path = '/www/wwwroot/test.py'

# 返回的是本文件的绝对路径 /www/wwwroot/test.py
print(os.path.abspath(__file__))

# 返回的是这个文件所在的目录/www/wwwroot/
print(os.path.dirname(os.path.abspath(__file__)))

# 返回的是这个目录的上一级目录/www/
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# 返回文件名test.py
print(os.path.basename(__file__))

# 返回list(多个路径)中,所有path共有的最长的路径,结果/wwwroot/pcf
print(os.path.commonprefix(['/wwwroot/pcff/','/wwwroot/pcfc/pc/']))

# 如果路径 path 存在,返回 True;如果路径 path 不存在,返回 False。返回结果True
print(os.path.exists(__file__))

# 把path中包含的"~"和"~user"转换成用户目录,比如用户目录为/home/python/,用户名在为python
os.path.expanduser(path)

# 输出结果为/home/python/www/wwwroot/test.py
print(os.path.expanduser('~%s' %path))

# 输出结果也是为/home/python/www/wwwroot/test.py
print(os.path.expanduser('~python%s' %path))


os.path.expandvars(path) 这个暂时不懂

# 返回最近访问这个文件(文件夹的)时间(浮点型秒数) #1581867996.7407362
print(os.path.getatime(path))

# 返回最近文件(文件夹)的修改时间 # 1581872316.0
print(os.path.getmtime(__file__))

# 在Unix系统上,返回的ctime是指定文件path的元数据上一次被修改的时间;在Windows系统上,返回的ctime是指定文件path创建的时间。
print(os.path.getctime(__file__))

#返回文件大小,字节为单位,如果文件不存在就返回错误
print(os.path.getsize(__file__))

# 判断是否为绝对路径,返回结果为True和False
print(os.path.isabs('www/wwwroot/'))

# 判断path是否为文件并且存在,如果path是文件且存在则返回True,反之返回False
print(os.path.isfile(path))

# 判断path是否为目录并且存在。如果path是目录且此目录存在,则返回True,反之返回False
print(os.path.isdir(path))

#  参考https://vimsky.com/examples/usage/python-os-path-islink-method.html
print(os.path.islink(path))

#  这个暂时不懂
print(os.path.ismount(path))

# 表示路径合并,合并之后不能带//
os.path.join(path1,path2,path3,...)

# 输出结果为/www/wwwroot/www2/www3/index.html
print(os.path.join('/www/wwwroot/','www2/www3/','index.html'))
# 输出结果为/www2/www3/index.html
print(os.path.join('/www/wwwroot/','/www2/www3/','index.html'))
# 输出结果为/index.html
print(os.path.join('/www/wwwroot/','/www2/www3/','/index.html'))
# 输出结果为www/wwwroot/www2/www3/index.html
print(os.path.join('www/wwwroot/','www2/www3/','index.html'))
# 输出结果为/www/wwwroot/www2/www3/index.html
print(os.path.join('/www/wwwroot/','www2/www3/','index.html'))

# 标准化路径名称的大小写。把大写文件夹转换成小写文件夹
print(os.path.normcase('/WWW/wwwroot/test.py'))

# 标准化路径。会把./去掉。输出www/wwwroot/test.py
print(os.path.normpath('./www/.///wwwroot/test.py'))

# 返回指定文件的标准路径,而非软链接所在的路径,输出/www/wwwroot/test.py
print(os.path.realpath('/www/wwwroot/test.py'))

# 输出/home/python/Desktop/new_meiduo_mall/new_meiduo_mall/new_meiduo_mall/apps/users/www/wwwroot/test.py
print(os.path.realpath('www/wwwroot/test.py'))

# 从start开始计算相对路径,暂时不太懂
print(os.path.relpath('www/wwwroot/test.py','test.py'))

#判断两个路径是否指向同一个文件或者文件夹(True,False),快捷方式不算,如果文件或者文件夹不存在则报错
print(os.path.samefile('/home/python/','/home/python/'))

#判断fp1和fp2是否指向同一文件,这个不太懂
print(os.path.sameopenfile(fp1, fp2))

# 把路径分割成 dirname 和 basename,返回一个元组,结果('/www/wwwroot', 'test.py')
print(os.path.split('/www/wwwroot/test.py'))

# 一般用在 windows 下,返回驱动器名和路径组成的元组
print(os.path.splitdrive('/www/wwwroot/test.py'))

# 分割路径,返回路径名和文件扩展名的元组,结果('/www/wwwroot/test', '.py')
print(os.path.splitext('/www/wwwroot/test.py'))

# 把路径分割为加载点与文件,这个不太懂
print(os.path.splitunc('/www/wwwroot/test.py'))

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


最后修改:2020年4月12日 19:26