简体   繁体   English

Shutil.move 不移动文件夹,而是移动文件夹的内容

[英]Shutil.move doesn't move folder, but contents of folder

I'm writing a script to reorganise a large data set so that it'll be mostly compatible with a database system we're currently implementing.我正在编写一个脚本来重新组织一个大型数据集,以便它与我们目前正在实施的数据库系统大部分兼容。 Most of our data is currently not organised in any meaningful way, although some folders are already marked in the way the script will.我们的大多数数据目前没有以任何有意义的方式组织,尽管一些文件夹已经按照脚本的方式进行了标记。 As such I've implemented a case to catch this.因此,我已经实施了一个案例来解决这个问题。

Let's call this case "INFO".我们称这种情况为“信息”。 In all but the first instance of this my script works fine, finds the "INFO_example[2,3,4....]" folder at the top level, and moves it to a new "INFO" folder that contains all of these examples.除了第一个实例之外,我的脚本工作正常,在顶层找到“INFO_example [2,3,4 ....]”文件夹,并将其移动到包含所有这些的新“INFO”文件夹例子。 However, for some reason in the first instance "INFO_example1" it instead takes the contents of the folder and dumps them in "INFO" instead.但是,由于某种原因,在第一个实例“INFO_example1”中,它取而代之的是文件夹的内容并将它们转储到“INFO”中。

I've attempted to debug the problem, but can't see any differences between the first and any other instances.我试图调试问题,但看不到第一个实例和任何其他实例之间的任何差异。 The folder path also doesn't appear any differently.文件夹路径也没有任何不同。

file_path = join(self.path, file)
try:
    move(file_path, self.info_path)
except shutil.Error:
    print("Trying to move", file_path, " didn't work")

I'm a little stumped to what's actually going on.我对实际发生的事情感到有些困惑。

I'd expect the "INFO_example1" folder to behave as all the others do, and just be moved into the top level "INFO" folder.我希望“INFO_example1”文件夹的行为与其他所有文件夹一样,只是被移动到顶级“INFO”文件夹中。

Currently, it's contents are moved into "INFO", and the "INFO_example1" folder appears to be deleted.目前,它的内容已移至“INFO”,“INFO_example1”文件夹似乎已被删除。

My print message also never fires.我的打印消息也永远不会触发。

So, the issue was that shutil.move would look at say "INFO_example1" and try to move it to "INFO", notice that "INFO" did not exist, and so would create it and put the contents of "INFO_example1" in to it instead of creating another folder within and then moving the contents into there.所以,问题是 shutil.move 会查看说“INFO_example1”并尝试将其移动到“INFO”,注意到“INFO”不存在,因此会创建它并将“INFO_example1”的内容放入它而不是在其中创建另一个文件夹,然后将内容移动到那里。

Due to the nature of the script creating "INFO" before trying to move files caused more issues, however, if I updated the section that moves the "INFO_example" folders, to look like:由于在尝试移动文件之前创建“INFO”的脚本的性质导致了更多问题,但是,如果我更新了移动“INFO_example”文件夹的部分,如下所示:

    file_path = join(self.path, file)
        try:
            if not isdir(self.info_path):
                mkdir(self.info_path)
            move(file_path, self.info_path)
        except shutil.Error:
            print("Trying to move", file_path, " didn't work")

Checking whether the "INFO" directory exists or not, and if not creating it but only when I'm attempting to move an "INFO_example" style folder gives the expected behaviour, at least for myself.检查“INFO”目录是否存在,如果不创建它,但只有在我尝试移动“INFO_example”样式文件夹时才会给出预期的行为,至少对我自己而言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM