Python 读取目录、文件
2021-06-24 03:06
标签:html list test .com time reference real code color 在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件、文件夹操作的方法。下面列出: 输出: 遍历文件和目录: Reference: [1] http://www.cnblogs.com/WonderHow/p/4403727.html Python 读取目录、文件 标签:html list test .com time reference real code color 原文地址:https://www.cnblogs.com/hoojjack/p/9671048.htmldef analyze_path(path):
print("abspath:", os.path.abspath(path))
print("basename:", os.path.basename(path))
print("dirname:", os.path.dirname(path))
print("exists:", os.path.exists(path))
print("atime:", os.path.getatime(path))
print("normcase:", os.path.normcase(path))
print("normpath:", os.path.normpath(path))
print("realpath:", os.path.realpath(path))
print("join:", os.path.join("F:\\test\\", os.path.basename(path)))
print("splitdrive:", os.path.splitdrive(path))
print("splitunc:", os.path.splitunc(path))
def main():
path = "E:\\Users\\Administrator\\eclipse-workspace\\com.leagsoft\\test\\example.csv"
analyze_path(path)
if __name__ == "__main__":
main()
abspath: E:\Users\Administrator\eclipse-workspace\com.leagsoft\test\example.csv
basename: example.csv
dirname: E:\Users\Administrator\eclipse-workspace\com.leagsoft\test
exists: True
atime: 1537200000.0
normcase: e:\users\administrator\eclipse-workspace\com.leagsoft\test\example.csv
normpath: E:\Users\Administrator\eclipse-workspace\com.leagsoft\test\example.csv
realpath: E:\Users\Administrator\eclipse-workspace\com.leagsoft\test\example.csv
join: F:\test\example.csv
splitdrive: (‘E:‘, ‘\\Users\\Administrator\\eclipse-workspace\\com.leagsoft\\test\\example.csv‘)
splitunc: (‘‘, ‘E:\\Users\\Administrator\\eclipse-workspace\\com.leagsoft\\test\\example.csv‘)
rootdir = ‘F:\data‘
list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isfile(path):
#你想对文件的操作
上一篇:Java大体介绍(超级短的那种)
下一篇:后缀排序