1 #!/usr/bin/python 2 ## A file handling script, including functions that could search file by key 3 ## and rename file by appending key value to the file names. 4 5 import os 6 import shutil 7 import copy 8 9 def get_INV_list(key_file):10 """ read key_file and get key values into list"""11 INV_file = open(key_file, 'r')12 key_list = []13 14 for num in INV_file.readlines():15 key_list.append([num[:-1]])16 17 INV_file.close()18 return key_list19 20 def write_result_list(key_list,out_file):21 """write updated key_list with reference into a file"""22 result = open(out_file,'w')23 24 for key in key_list:25 result.write('########'.join(key)+'\n')26 27 result.close()28 29 def search(key_list,search_dir):30 """search key value in all files in search directory,31 return a list with key reflect file name"""32 33 ##my original workaround34 #result_list = []35 #for item in key_list:36 # result_list.append(item[:])37 ##A simpler code38 #result_list = [item[:] for item in key_list]39 ## or 40 #result_list = [list(item) for item in key_list]41 #best solution is copy.deepcopy()42 result_list = copy.deepcopy(key_list)43 44 for file in os.listdir(search_dir):45 search_file = open(search_dir+'/'+file,'r')46 search_str = search_file.read()47 search_file.close()48 for key in result_list:49 if key[0] == search_str[86:92]: ##only check INV#50 #if key[0] in search_str:51 key.append(file)52 53 return result_list54 55 def rename(result_list,input_dir,output_dir):56 """ rename files in input directory and move to output directory,57 return a list of file names those not moved."""58 for item in result_list:59 if len(item) > 1:60 for filename in item[1:]:61 # move files to another folder and rename them62 # name format could be modified if necessary63 shutil.move(input_dir+'/'+filename,64 output_dir+'/'+filename+'.NZ'+item[0])65 66 return os.listdir(input_dir)67 68 xlist = get_INV_list('INV_Num')69 ylist = search(xlist,'InputFiles')70 rest_list = rename(ylist,'InputFiles','OutputFiles')71 write_result_list(ylist,'resultList')
总结:
1. 之前已经写过一个简单脚本,对文件做关键字搜索以提取文件名,一周之后类似的需求只是需要修改对应文件名,不懂得代码重用,结果又折腾了两个小时才算是写好,今天有空将两段代码模块化,重构整个代码。
2. 学习到shutil, copy 模块,尤其是copy.deepcopy, 这个写法是从Stack Overflow 上提问才得到的,这是第一次提问,感觉国外的programmer回复很快,也很专业,以后要多去溜达学习。
3. 拼凑出来的代码,可以解决实际工作中的问题,这很重要,时不时的review这段代码,肯定还有许多可以改进之处。