python-获取目录内所有文件的绝对路径,获取文件大小并输出到txt文件中
代码如下:
import os dirpath = 'D:\\' t = [] for root,dirs,files in os.walk(dirpath): for file in files: temp = os.path.join(root,file).replace('\\','\\\\') if os.path.isfile(temp): t.append(temp) if (os.path.exists("filesize.txt")): os.remove("filesize.txt")
f=open("filesize.txt","a",encoding='utf-8') i = 0 while i < len(t): f.writelines('size:'+ str(os.path.getsize(t[i])) + "\n" + t[i]+ "\n") i = i +1 f.close()