【发布时间】:2018-10-19 19:21:27
【问题描述】:
我正在尝试从一个目录创建一个列表,更改名称以匹配另一个命名约定,并使用该列表引用不同目录中的文件以移动到另一个目录。我已经做到了这一点,但 shutil.move() 正在移动整个目录,而不是在列表中搜索匹配的文件名。
我不确定如何进行,任何帮助将不胜感激!
#Goal is to create list from the reference folder,
#change names in list to match naming convention in the source folder,
#and move only those files to the destination folder.
import os
import shutil
r = input("Reference Folder")
s = input("Source Folder")
d = input("Destination folder")
os.chdir(r)
if not os.path.exists(d):
os.mkdir(d)
#creating filelist from reference folder
filelist = []
for root, dirs, files in os.walk(".", topdown = False):
for file in files:
filelist.append(file)
#changing filelist to match source folder's naming convention.
os.chdir(s)
filelist = [f.replace('filling_mask', 'tex') for f in filelist]
print("Moving these")
print(filelist)
for t in filelist:
shutil.move(s, d)
【问题讨论】:
-
你的变量名不是很具描述性(你应该改变它们),但它不应该是
t你试图移动,而不是s? -
最后一行不是
shutil.move(t, d)吗?