编码方面是全新的(2-3周前开始),我迫切需要帮助...
我正在尝试将目录(比如dir1
)中的某些文件(* .tif文件)移动到另一个目录(dir2
)中(如果它们在列表中)。就我而言,我们将此列表称为duplicates.csv
。
编辑:这就是重复项.csv的概念。由于尚未允许我嵌入,因此这里是链接:image of duplicates.csv
所以现在我要加载重复列表:
import os
import pandas as
import shutil
# Load the list from CSV
duplicates = pd.read_csv(r'\\galaxy\WETS_ScanTest\duplicates.csv', usecols=[1])
# Convert to pandas dataframe to list
duplicates = df_duplicates.values.tolist()
# These are the directories:
dir1 = r'\\galaxy\WETS_ScanTest\TEMP2' # All the files are here
dir2 = r'\\galaxy\WETS_ScanTest\TEMP3' # Move to here IF it matches the `duplicates` list (aka: duplicates.csv)
当我尝试移动文件时,
# Move the files to a different directory
for root, dirs, files in os.walk(dir1, topdown=True):
for i in files:
if i in duplicates:
shutil.move((os.path.join(root, i)), dir2)
由于某种原因,我无法使它正常工作。
编辑:它没有给出错误消息,但是重复的文件没有移到dir2
对此新手的任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
import os
from os import path
import shutil
dir1 = r'\\galaxy\WETS_ScanTest\TEMP2' # All the files are here
dir2 = r'\\galaxy\WETS_ScanTest\TEMP3' # Move to here IF it matches the `duplicates` list (aka: duplicates.cs
files = [i for i in os.listdir(dir1) if i.endswith(("csv", "txt")) and path.isfile(path.join(dir1, i))]
for f in files:
shutil.move(path.join(dir1, f), dir2)
您可以使用此代码段
答案 1 :(得分:0)
您可以更改
duplicates_tuple = ("csv", "txt")
files = [i for i in os.listdir(dir1) if i.endswith(duplicates_tuple) and path.isfile(path.join(dir1, i))]
至
duplicates_tuple = tuple(pd.read_csv(r'\galaxy\WETS_ScanTest\duplicates.csv', usecols=[1]).values.tolist())
files = [i for i in os.listdir(src) if i in (duplicates_tuple) and path.isfile(path.join(src, i))]