今天继续整理原来写的 python 代码,下面是获取文件信息的 python 处理代码。
#!/usr/bin/env python2#-*-encoding:utf-8-*-import os,sysdef listdir(dir,file):file.write(dir +'\n')fielnum =0list = os.listdir(dir)#列出目录下的所有文件和目录for line in list:filepath = os.path.join(dir,line)if os.path.isdir(filepath):#如果filepath是目录,则再列出该目录下的所有文件myfile.write(' '+ line +'//'+'\n')for li in os.listdir(filepath):myfile.write(' '+li +'\n')fielnum = fielnum +1elif os.path:#如果filepath是文件,直接列出文件名myfile.write(' '+line +'\n')fielnum = fielnum +1myfile.write('all the file num is '+ str(fielnum))dir = raw_input('please input the path:')myfile = open('list.txt','w')listdir(dir,myfile)myfile.close()
G:\codes\python\file_infolist.txtlist2.txtlist_filenames.pylist_filenames2.pylist_files+.pytest//g.txtmmm.pptxtest2//list.txttest3all the file num is9
#!/usr/bin/env python2#-*-encoding:utf-8-*-"""os.walk(path),遍历path,返回一个对象,他的每个部分都是一个三元组,('目录x',[目录x下的目录list],目录x下面的文件)"""import osdef walk_dir(dir,fileinfo,topdown=True):for root, dirs, files in os.walk(dir, topdown):for name in files:print(os.path.join(name))fileinfo.write(os.path.join(root,name)+'\n')for name in dirs:print(os.path.join(name))fileinfo.write(' '+ os.path.join(root,name)+'\n')dir = raw_input('please input the path:')dir = r'G:\codes\python\file_info'fileinfo = open('list2.txt','w')walk_dir(dir,fileinfo)fileinfo.close()
>>>================================ RESTART ================================>>>please input the path:G:\codes\python\file_infolist.txtlist2.txtlist_filenames.pylist_filenames2.pylist_files+.pytesttest2g.txtmmm.pptxlist.txttest3234.txt
G:\codes\python\file_info\list.txtG:\codes\python\file_info\list2.txtG:\codes\python\file_info\list_filenames.pyG:\codes\python\file_info\list_filenames2.pyG:\codes\python\file_info\list_files+.pyG:\codes\python\file_info\testG:\codes\python\file_info\test2G:\codes\python\file_info\test\g.txtG:\codes\python\file_info\test\mmm.pptxG:\codes\python\file_info\test2\list.txtG:\codes\python\file_info\test2\test3G:\codes\python\file_info\test2\test3\234.txt
#!/usr/bin/env python2#-*-encoding:utf-8-*-import osimport sysimport md5def walk_dir(dir,fileinfo,topdown=True): for root, dirs, files in os.walk(dir, topdown): for name in files: path = os.path.join(root,name) md5v = sumfile(path) newpath = path.replace(dir,'') fileinfo.write(newpath + ':' + md5v + '\n')def sumfile(fpath): m = md5.new() fobj = open(fpath) while True: d = fobj.read(8096) if not d: break m.update(d) return m.hexdigest()#获取脚本文件的当前路径def cur_file_dir(): #获取脚本路径 path = sys.path[0] #判断为脚本文件还是py2exe编译后的文件,如果是脚本文件,则返回的是脚本的目录,如果是py2exe编译后的文件,则返回的是编译后的文件路径 if os.path.isdir(path): return path elif os.path.isfile(path): return os.path.dirname(path)#打印结果print cur_file_dir()def main(): #dir = raw_input('please input the path:') dir = cur_file_dir() fileinfo = open('list3.txt','w') walk_dir(dir,fileinfo)if __name__ == '__main__': main()
\list.txt:0a732356981d24beab0d0c1c4092b2a7\list2.txt:6cebc24b3a996be27c732557a2ce545f\list3.txt:d41d8cd98f00b204e9800998ecf8427e\list_filenames.py:9201c1eefcaf2fd04478cc2b6430c678\list_filenames2.py:ca3f74bdb0a5485dd5d95d77da80141b\list_files+.py:720d569d2f07f0c80e0d695db31749ab\test\g.txt:d41d8cd98f00b204e9800998ecf8427e\test\mmm.pptx:6f0e81ad84c22838337f0f619080e7f2\test2\list.txt:d41d8cd98f00b204e9800998ecf8427e\test2\test3\234.txt:d41d8cd98f00b204e9800998ecf8427e