Python extracts the source code of specified file operation under nested folders

How to extract the contents or files of known file names in multiple nested folders? This article is about Python extracting source code examples of specified file operations under nested folders .

Version: python2.6.6
Environment: window xp

Python code writing ideas:
First, place the two py files under the required folder, run filelist.py first, and then run.py. The functions of these two files are as follows:
filelist.py:
Get the file list under the current folder and generate a names.txt file. This file can be modified manually to set the file list to be extracted.
run.py:
After running, you will be prompted to enter the target folder, and then the program will automatically transfer the file to the specified directory based on names.txt.

filelist.py file Python source code :


#encoding: utf-8
 
 
 
import os
 
filenames = os.listdir(os.getcwd())
 
for name in filenames:
    filenames[filenames.index(name)] = name
out = open('names.txt','w')
for name in filenames:
         
        filetype = name[len(name)-3:len(name)]
        ##更加后缀名拦截一些不需要的文件
        ##拦截规则可以自定定义
        if  filetype =='pbl':
                print ("pbl 文件被拦截 -->"+name)
        elif  filetype =='pbr':
                print ("pbr 文件被拦截 -->"+name)
        elif  filetype == 'pbw':
                print ("pbw 文件被拦截 -->"+name)
        elif  filetype == 'pbt':
                print ("pbt 文件被拦截 -->"+name)
        else:
                print(name[len(name)-3:len(name)])
                out.write(name+'\n')
out.close()

The source code of run.py is as follows:

#encoding: utf-8
 
import os
import sys
import shutil
import time
 
 
if __name__ == "__main__":
        curdir = os.getcwd()
        namefile = "names.txt"
        if os.path.isfile(namefile):
                print("文件列表以检测到,正在读取....")
        else:
                print("未找到names.txt文件,程序结束")
                ##sys.exit()
                 
        tag = ''
 
        while ( not os.path.isdir(tag)):
                tag = raw_input('输入目标路径:')
                if not os.path.isdir(tag):
                        os.mkdir(tag)
                         
        tag = tag+'\\'    
        nfile = open(namefile)
        for line in nfile:        
                src = line[:-1]
                 
                shutil.copy(src,tag)
                print(src+' 复制到 '+tag)
        nfile.close

Guess you like

Origin blog.csdn.net/lmrylll/article/details/131961835