Python 将文件列表复制到新目录,检查是否存在重复项
所以在想了几天为什么我没有得到任何帮助后,我发现我的问题已经结束了。我用粗体字提出了我的问题。有一个答案,我用它来帮助我,但由于某种原因,它被删除,所以现在我回来了 下面是我的脚本当前的运行情况:Python 将文件列表复制到新目录,检查是否存在重复项,python,list,loops,directory,copy,Python,List,Loops,Directory,Copy,所以在想了几天为什么我没有得到任何帮助后,我发现我的问题已经结束了。我用粗体字提出了我的问题。有一个答案,我用它来帮助我,但由于某种原因,它被删除,所以现在我回来了 下面是我的脚本当前的运行情况: 通读每行都有\path\文件名的文本文档 这些文件将添加到“我的代码”中的列表中 使用此列表为列表中的每个文件搜索根目录 将这些文件复制到新目录 我希望它做什么: 如果文件已在目标目录中,请在扩展名之前添加一个数字 让我困惑的是,列表中的文件可能具有相同的名称和扩展名,但具有不同的文件路径。这
- 通读每行都有\path\文件名的文本文档
- 这些文件将添加到“我的代码”中的列表中
- 使用此列表为列表中的每个文件搜索根目录
- 将这些文件复制到新目录
- 如果文件已在目标目录中,请在扩展名之前添加一个数字
itemList1st = []
# txtFile = input("Copy and paste .txt file path here")
ssfFilePath = listSSFFiles.replace(os.sep, "/")
# Open then read the .txt file, line by line
with open(ssfFilePath, "r") as openSSFFilePath:
# if destDir doesn't exist, make it
try:
if not os.path.exists(destDir):
print("Destination folder for ssf files not created. Making one at {}".format(destDir))
os.makedirs(destDir)
except():
print("error")
# make list of files found in text document, remove the extension
sep = '.'
with openSSFFilePath as searcher:
for row in searcher:
itemList1st.append(row.split(sep, 1)[0])
print("Raw file name list: ", itemList1st)
itemList2nd = [item + ".SSF" for item in itemList1st] # add the .ssf extension so only raw files are found
print("Files with extension: ", itemList2nd) # list is
finalList = list(set(itemList2nd))
sep = "_"
for root, dirs, files in os.walk(rootDir):
for eachItem in files:
if eachItem in finalList: # this list has .ssf extensions
baseName, fileExt = os.path.splitext(eachItem)
if not os.path.isfile(destDir):
shutil.copy(eachItem, destDir)
else:
newName = [baseName, 1, fileExt]
shutil.copy(newName, destDir)
你的问题是什么?