我的源文件夹中有 120 个文件,我需要将它们移动到新目录(目标)。根据文件名中的
made
,目标是我编写的函数中的 string
。例如,这是我使用的函数。
path ='/path/to/source'
dropbox='/path/to/dropbox'
files = = [os.path.join(path,i).split('/')[-1] for i in os.listdir(path) if i.startswith("SSE")]
sam_lis =list()
for sam in files:
sam_list =sam.split('_')[5]
sam_lis.append(sam_list)
sam_lis =pd.unique(sam_lis).tolist()
# Using the above list
ID = sam_lis
def filemover(ID,files,dropbox):
"""
Function to move files from the common place to the destination folder
"""
for samples in ID:
for fs in files:
if samples in fs:
desination = dropbox + "/"+ samples + "/raw/"
if not os.path.isdir(desination):
os.makedirs(desination)
for rawfiles in fnmatch.filter(files, pat="*"):
if samples in rawfiles:
shutil.move(os.path.join(path,rawfiles),
os.path.join(desination,rawfiles))
在该函数中,我根据从文件列表派生的 ID 创建目标文件夹。当我第一次尝试运行这个时,它让我感到震惊。 然而,后来当我检查源代码时,所有以
FILE NOT exists error
开头的文件都丢失了。一开始,文件就在那里。我想在这里得到一些见解;是否
SSE
os.shutil.move