2011-08-18 162 views
2

我有兩個源文件路徑:的Python:移動從文件夾和子文件夾中的文件到另一個類似的目錄

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

和一個目的路徑:

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

所有這些路徑的子文件夾是相同的。他們是10-15文件夾,有3位數字(例如233)。

如果有任何文件位於源路徑的這些子文件夾中,我想將它們從該目錄中切出並使用與源相同的路徑將它們發送到目標目錄。我是新的python,所以任何幫助將不勝感激。

回答

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. 
相關問題