Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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通过shutil.move(src,dst)移动文件夹内容在目标文件夹中创建整个源文件夹_Python_Shutil - Fatal编程技术网

python通过shutil.move(src,dst)移动文件夹内容在目标文件夹中创建整个源文件夹

python通过shutil.move(src,dst)移动文件夹内容在目标文件夹中创建整个源文件夹,python,shutil,Python,Shutil,然而,我认为shutil.move(src,dst)可以完成这项工作,具体如下: move(src,dst)递归地将文件或目录(src)移动到 另一个位置(dst) 如果目标是一个现有目录,那么src将被移动到内部 那个目录。如果目标已存在,但不是 目录,它可能会被覆盖,具体取决于os.rename()语义 这与我的案例略有不同,如下所示: 搬迁前: 搬迁后: 这不是我想要的,我希望暂存文件夹中的所有内容都移动到“final”文件夹下,我不需要“staging”文件夹本身 如果您能帮忙,我们

然而,我认为shutil.move(src,dst)可以完成这项工作,具体如下:

move(src,dst)递归地将文件或目录(src)移动到 另一个位置(dst)

如果目标是一个现有目录,那么src将被移动到内部 那个目录。如果目标已存在,但不是 目录,它可能会被覆盖,具体取决于os.rename()语义

这与我的案例略有不同,如下所示:

搬迁前:

搬迁后:

这不是我想要的,我希望暂存文件夹中的所有内容都移动到“final”文件夹下,我不需要“staging”文件夹本身

如果您能帮忙,我们将不胜感激


谢谢。

您可以使用
os.listdir
然后将每个文件移动到所需的目标位置

Ex:

import shutil
import os

for i in os.listdir(staging_folder):
    shutil.move(os.path.join(staging_folder, i), final_folder)

您可以使用
os.listdir
,然后将每个文件移动到所需的目标

Ex:

import shutil
import os

for i in os.listdir(staging_folder):
    shutil.move(os.path.join(staging_folder, i), final_folder)

结果表明,路径不正确,因为它包含\t,这被误解了

我最终使用了shutil.move+shutil.copy22

for i in os.listdir(staging_folder):
    if not os.path.exists(final_folder):
        shutil.move(os.path.join(staging_folder, i), final_folder)
    else:
        shutil.copy2(os.path.join(staging_folder, i), final_folder)
然后清空旧文件夹:

def emptify_staging(self, folder):
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
                # elif os.path.isdir(file_path): shutil.rmtree(file_path)
        except Exception as e:
            print(e)

结果表明,路径不正确,因为它包含\t,这被误解了

我最终使用了shutil.move+shutil.copy22

for i in os.listdir(staging_folder):
    if not os.path.exists(final_folder):
        shutil.move(os.path.join(staging_folder, i), final_folder)
    else:
        shutil.copy2(os.path.join(staging_folder, i), final_folder)
然后清空旧文件夹:

def emptify_staging(self, folder):
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
                # elif os.path.isdir(file_path): shutil.rmtree(file_path)
        except Exception as e:
            print(e)

谢谢,但它不会在StagingHanks中移动子文件夹,但不会在StagingHanks中移动子文件夹