博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python: file_handling 解决工作中出现的文件处理需求
阅读量:6903 次
发布时间:2019-06-27

本文共 2716 字,大约阅读时间需要 9 分钟。

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这段代码,肯定还有许多可以改进之处。

转载于:https://www.cnblogs.com/Alex-Python-Waiter/archive/2012/07/09/2583631.html

你可能感兴趣的文章
自动类型转换
查看>>
C# winfrom 当前程序内存读取和控制
查看>>
电话号码分身
查看>>
Redis持久化方案
查看>>
Unity保存序列化数据
查看>>
测试jupyter notebook导出md格式的兼容性
查看>>
【转】WPF MultiBinding 和 IMultiValueConverter
查看>>
解决springMVC文件上传报错: The current request is not a multipart request
查看>>
Struts2国际化-getText()方法
查看>>
实时监听组件中路由的变化
查看>>
MnasNet:迈向移动端机器学习模型设计的自动化之路
查看>>
选项卡的JS
查看>>
青蛙的约会(扩展欧几里得)
查看>>
Asia Yokohama Regional Contest 2018 C题 - Arithmetic Progressions(思维)
查看>>
UVa 101 - The Blocks Problem STL
查看>>
计算机专业术语
查看>>
Leetcode-探索 | 移动零
查看>>
DBI 数据库模块剖析:Perl DBI 数据库通讯模块规范,工作原理和实例
查看>>
Tesseract+opencv+VS+win实现OCR
查看>>
android在activity中锁屏解锁后重走OnCreate的问题的解决办法
查看>>