尝试使用 python shutdown.move 移动文件时丢失文件

问题描述 投票:0回答:2

我的源文件夹中有 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
  1. 将文件移动到诸如临时文件夹之类的位置而不是目标文件夹?
    在任何情况下
  2. os.shutil.move
  3. 是否会从源中删除文件?
    有什么方法可以测试我的脚本以查找丢失文件的潜在原因吗?
  4. 非常感谢任何帮助或建议?

python shutil
2个回答
1
投票

例如,如果运行下面的代码,将会发生与操作描述类似的情况:

os.shutil.move

请注意 dst_folder 只是一个文件夹。它应该是 dst_folder + file_name。这将导致操作员在他的问题中描述的情况。我在此处的链接上找到了类似的内容,并对问题进行了更详细的解释:
File moving error with Python


0
投票

© www.soinside.com 2019 - 2024. All rights reserved.