基于字典的python移动文件
我正在尝试从/home/wiet/import/directory(recursive)search下的文件夹中移动特定文件(仅以*.txt结尾)。但是我想根据字典来做,例如在文件夹/home/wiet/import/OS中,只有以*windows.txt或*rhel.txt结尾的文件应该被移动到其他文件中,我想在控制台中获得关于不匹配文件的信息(最好将它们添加到一些列表中)。现在,我在下面有一个,但我无法将它与字典匹配,并且我在将正确的文件复制到另一个目录时遇到问题基于字典的python移动文件,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,我正在尝试从/home/wiet/import/directory(recursive)search下的文件夹中移动特定文件(仅以*.txt结尾)。但是我想根据字典来做,例如在文件夹/home/wiet/import/OS中,只有以*windows.txt或*rhel.txt结尾的文件应该被移动到其他文件中,我想在控制台中获得关于不匹配文件的信息(最好将它们添加到一些列表中)。现在,我在下面有一个,但我无法将它与字典匹配,并且我在将正确的文件复制到另一个目录时遇到问题 import sys im
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')
我不知道你想要什么 希望这有助于:
....
....
for key in tech:
if name in tech[key]:
'''do something'''
....
....
一个
walk()
内部的walk()
对我来说似乎有点奇怪,外部的walk不是已经访问了所有目录吗?好的,但我仍然在寻找如何将路径与字典键及其值匹配错误消息/观察到的结果是什么?带有copy2()
的最后一行看起来可疑,txtcorrect
仍然是一个列表,对吗?它与问题有什么关系?您可以使用相同的变量名,或者添加更多的上下文吗?