繁体   English   中英

使用 shutil.move 移动文件

[英]Using shutil.move to move files

我正在尝试编写一个脚本来将文件从一个位置移动到机器上本地的另一个位置。 我正在为 Mac OS 编写这个脚本。

代码:

#!/usr/bin/env python

import shutil
import _osx_support
import os

src = "Macintosh HD//Users//jerel//Desktop//Testing/"
dst = "Macintosh HD//Users//jerel//Desktop/"

shutil.move(src=src + "Testing doc 2.pdf", dst=dst)
Error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 788, in move
    os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: 'Macintosh HD//Users//jerel//Desktop//Testing/Testing doc 2.pdf' -> 'Macintosh HD//Users//jerel//Desktop/'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jerel/Desktop/moving files .py", line 11, in <module>
    shutil.move(src=src + "Testing doc 2.pdf", dst=dst)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 802, in move
    copy_function(src, real_dst)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 432, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 261, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'Macintosh HD//Users//jerel//Desktop//Testing/Testing doc 2.pdf'

更新代码:

#!/usr/bin/env python

import shutil
import _osx_support
import os

src = "/Users/jerel/Desktop/Testing/"
dst = "/Users/jerel/Desktop/"

shutil.move(src=src + "Testing doc 2.pdf", dst=dst)

更新 2:

#!/usr/bin/env python

import shutil
import _osx_support
import os

src = "/Users/jerel/Desktop/Testing/"
dst = "/Users/jerel/Desktop/"

shutil.move(src=src + "Testing doc 2", dst=dst)
Error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 788, in move
    os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/jerel/Desktop/Testing/Testing doc 2' -> '/Users/jerel/Desktop/Testing doc 2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jerel/Desktop/moving files .py", line 11, in <module>
    shutil.move(src=src + "Testing doc 2", dst=dst)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 802, in move
    copy_function(src, real_dst)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 432, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 261, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/jerel/Desktop/Testing/Testing doc 2'

您的路径不应包含“Macintosh HD”,而应仅以 /Users 开头

编辑:另外,将路径中的双斜杠更改为单斜杠

确保您已经正确安装了shutil 模块。 您可以使用以下命令。

pip3 install pytest-shutil

然后使用以下代码。

import shutil

shutil.move(r"/Users/jerel/Desktop/Testing/Testing doc 2.pdf", r"/Users/jerel/Desktop")

暂无
暂无

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

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