在python下移动文件

Ash*_*Ash 8 python windows move

我对在python下移动的文件感到困惑.在Windows命令行下,如果我有目录c:\ a和目录c:\ b,我可以这样做

move c:\a c:\b
Run Code Online (Sandbox Code Playgroud)

将a移动到b的结果是目录结构c:\ b\a

如果我尝试使用os.rename或shutil.move:

os.rename("c:/a", "c:/b")
Run Code Online (Sandbox Code Playgroud)

我明白了

WindowsError: [Error 17] Cannot create a file when that file already exists
Run Code Online (Sandbox Code Playgroud)

如果我在c:\ a下移动一个文件,它可以工作.

在python中如何将目录移动到另一个现有目录?

sun*_*ang 16

os.rename("c:/a", "c:/b/a") 
Run Code Online (Sandbox Code Playgroud)

相当于

move c:\a c:\b
Run Code Online (Sandbox Code Playgroud)

在Windows命令行下


Key*_*Key 8

您可以尝试使用Shutil模块.

  • 特别是shutil.move(srcFile,destFile) (2认同)