Python:将文件从文件夹和子文件夹移动到另一个类似的目录

时间:2011-08-18 19:33:21

标签: python

我有两个源文件路径:

C:\Same\Path\To\File\unknown\
C:\Same\Path\To\File\unrecognized\

一条目的地路径:

C:\Same\Path\To\File\Import

所有这些路径的子文件夹都是相同的。它们是10-15个具有3位数名称的文件夹(例如233)。

如果源路径的这些子文件夹中有任何文件,我想从该目录中删除它们,并将它们发送到目标目录,其路径与源路径相同。我是python的新手,所以任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:3)

我认为你想要的是shutil.movepy3k docs):

Help on function move in module shutil:

move(src, dst)
Recursively move a file or directory to another location. This is
similar to the Unix "mv" command.

If the destination is a directory or a symlink to a directory, the source
is moved inside the directory. The destination path must not already
exist.

If the destination already exists but is not a directory, it may be
overwritten depending on os.rename() semantics.

If the destination is on our current filesystem, then rename() is used.
Otherwise, src is copied to the destination and then removed.
A lot more could be done here...  A look at a mv.c shows a lot of
the issues this implementation glosses over.

答案 1 :(得分:1)

查看shutil模块。