Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将文件列表复制到新目录,检查是否存在重复项_Python_List_Loops_Directory_Copy - Fatal编程技术网

Python 将文件列表复制到新目录,检查是否存在重复项

Python 将文件列表复制到新目录,检查是否存在重复项,python,list,loops,directory,copy,Python,List,Loops,Directory,Copy,所以在想了几天为什么我没有得到任何帮助后,我发现我的问题已经结束了。我用粗体字提出了我的问题。有一个答案,我用它来帮助我,但由于某种原因,它被删除,所以现在我回来了 下面是我的脚本当前的运行情况: 通读每行都有\path\文件名的文本文档 这些文件将添加到“我的代码”中的列表中 使用此列表为列表中的每个文件搜索根目录 将这些文件复制到新目录 我希望它做什么: 如果文件已在目标目录中,请在扩展名之前添加一个数字 让我困惑的是,列表中的文件可能具有相同的名称和扩展名,但具有不同的文件路径。这

所以在想了几天为什么我没有得到任何帮助后,我发现我的问题已经结束了。我用粗体字提出了我的问题。有一个答案,我用它来帮助我,但由于某种原因,它被删除,所以现在我回来了

下面是我的脚本当前的运行情况:

  • 通读每行都有\path\文件名的文本文档
  • 这些文件将添加到“我的代码”中的列表中
  • 使用此列表为列表中的每个文件搜索根目录
  • 将这些文件复制到新目录
我希望它做什么:

  • 如果文件已在目标目录中,请在扩展名之前添加一个数字
让我困惑的是,列表中的文件可能具有相同的名称和扩展名,但具有不同的文件路径。这是因为我使用的根目录中有几个文件夹(例如:files\billy\U020620.txt和files\joey\U020620.txt)

因此,我想要的结果是复制文件,但复制的文件最终被命名为U020620_1.txt。我该怎么做?

最后一部分是一切都搞砸了。这项工作仍在进行中

这是我的密码:

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)


你的问题是什么?