Heim  >  Artikel  >  Backend-Entwicklung  >  Python-Dateipfadoperation

Python-Dateipfadoperation

巴扎黑
巴扎黑Original
2016-12-03 10:45:211558Durchsuche

import os.path
path = '/home/vamei/doc/file.txt'
print(os.path.basename(path))    # 查询路径中包含的文件名
print(os.path.dirname(path))     # 查询路径中包含的目录
info = os.path.split(path)       # 将路径分割成文件名和目录两个部分,放在一个表中返回
print(info)
path2 = os.path.join('/', 'home', 'vamei', 'doc', 'file1.txt')  # 使用目录名和文件名构成一个路径字符串,字符串拼接 输出:/home\vamei\doc\file1.txt
p_list = [path, path2]
print(os.path.commonprefix(p_list))    # 查询多个路径的共同部分
os.path.normpath(path)   # 去除路径path中的冗余。比如'/home/vamei/../.'被转化为'/home'
print(os.path.exists(path))    # 查询文件是否存在
print(os.path.getsize(path))   # 查询文件大小
print(os.path.getatime(path))  # 查询文件上一次读取的时间
print(os.path.getmtime(path))  # 查询文件上一次修改的时间
print(os.path.isfile(path))    # 路径是否指向常规文件
print(os.path.isdir(path))     # 路径是否指向目录文件

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn