您的位置:首页 > 编程语言 > Python开发

Python实现批处理

2015-12-30 16:30 831 查看
最近搞一个项目需要处理很多文本,分散在很多不同的文件夹里面,要是一个个copy复制,不仅很慢而且很容易出错,况且那是一件多么木有意义的事情。

用Python处理简单了很多,而且不会出现问题。

1、首先要遍历文件夹使用os.walk

2、满足条件的文件进行copy,copy到指定目录,这时可以使用很多种方法。个人使用的os.system('copy')

3、文件处理使用自己写的C语言函数,需要同上,很多方法。

使用过程中还有一些简单的正则表达式。代码贴上来,以后使用的时候可以直接copy了

[python] view
plaincopyprint?

import os  

import re  

import win32api  

Pattern=0  

def Mypattern(Pfilename):  

    return len(Pattern.findall(Pfilename))  

def VisitDir(path):    

    global SubNum  

    for root,Dirs,files in os.walk(path):  

        for file in files:  

            if Mypattern(file):  

                SubNum+=1  

                print os.path.join(root,file)  

                filepath=os.path.join(root,file)  

                filepath=filepath+r' '+r'D:\Xpath\thesaugb.dbz'  

                print filepath  

#                os.system ("copy %s %s"%(filepath,filename2))  

                os.system("D:\Test\NewConsole.exe %s"%filepath)  

   

if __name__=="__main__":  

    path=r"D:\Pkw"  

    filename2=r'D:\Test'  

    RecordText=open(r'D:\Record.txt','r+')  

    Pattern=re.compile(r'^[gG][fF]\w*.sgs.txt$')  

    SubNum=0  

    VisitDir(filename2)  

#    win32api.ShellExecute(0,'open',r'D:\Xpath\NewConsole.exe',filepath,'',1)  

    print SubNum
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: