标签:html list test .com time reference real code color
在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件、文件夹操作的方法。下面列出:
def 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): #你想对文件的操作
Reference:
[1] http://www.cnblogs.com/WonderHow/p/4403727.html
标签:html list test .com time reference real code color
原文地址:https://www.cnblogs.com/hoojjack/p/9671048.html