基于字典的python文件移动

2024-10-02 00:24:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从/home/wiet/import/directory(recursive)search下的文件夹中移动特定文件(仅以*.txt结尾)。但是我想根据字典来做,例如在folder/home/wiet/import/OS中,只有结尾的文件*windows.txt文件,或*瑞尔.txt应该移动关于其他文件,我想在控制台中有关不匹配文件的信息(最好将它们添加到某个列表中)。现在我已经在下面找到了这个,但是我无法将它与字典匹配,并且我在将正确的文件复制到另一个目录时遇到了问题

import sys
import os
import shutil
import glob
import fnmatch
tech = {"OS": ["rhel.txt", "windows.txt"]
        "DB" : ["mysql.txt","oracle.txt","postgres.txt","postgresql.txt"],
        "WEB" : ["iis.txt" , "apache.txt", "tomcat.txt" ] ,
        }

fileslist = []
txtcorrect = []
destin = "/home/wiet/import"
print "part1 \n \n \n"


path = os.path.join(destin)
for path, subdirs, files in os.walk(destin):
    for name in files:
        if name.endswith('.txt'):
        print "found: " + name
        for root, dirnames, filenames in os.walk(path):
            for filename in fnmatch.filter(filenames, '*.txt'):
                txtcorrect.replace(os.path.join(root, filename))
                print txtcorrect
                shutil.copy2( txtcorrect , '/home/wiet/out')

Tags: 文件pathnameinimporttxthomefor

热门问题