找到每个子目录中的最大文件,并将文件移动到新目录

时间:2022-06-03 02:23:04

I am very new to python and stack overflow. In a directory, I have a list of many sub directories with many files within each. I am trying to figure out a way to find the largest file within each sub directory, and copy (move) these files to a new directory. Is this possible, any help is much appreciated.

我是python和堆栈溢出的新手。在一个目录中,我有一个包含许多子目录的列表,每个子目录中都有许多文件。我试图找到一种方法来找到每个子目录中的最大文件,并将这些文件复制(移动)到一个新目录。这是可能的,任何帮助都非常感谢。

1 个解决方案

#1


You'll want to look at the os module, and the os.path submodule in that. os.listdir and os.path.getsize will both be useful. You can use os.rename to move a file, or, for maximum compatibility, can use the shutil module's shutil.move, which will help if you're moving things from one filesystem to another.

您将要查看os模块和os.path子模块。 os.listdir和os.path.getsize都很有用。您可以使用os.rename来移动文件,或者,为了获得最大的兼容性,可以使用shutil模块的shutil.move,这将有助于您将内容从一个文件系统移动到另一个文件系统。

#1


You'll want to look at the os module, and the os.path submodule in that. os.listdir and os.path.getsize will both be useful. You can use os.rename to move a file, or, for maximum compatibility, can use the shutil module's shutil.move, which will help if you're moving things from one filesystem to another.

您将要查看os模块和os.path子模块。 os.listdir和os.path.getsize都很有用。您可以使用os.rename来移动文件,或者,为了获得最大的兼容性,可以使用shutil模块的shutil.move,这将有助于您将内容从一个文件系统移动到另一个文件系统。