【问题标题】:Using shutil.move to move files使用 shutil.move 移动文件
【发布时间】:2020-07-19 20:52:11
【问题描述】:

我正在尝试编写一个脚本来将文件从机器上的一个位置本地移动到另一个位置。我正在为 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'

【问题讨论】:

    标签: python macos file move shutil


    【解决方案1】:

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

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

    【讨论】:

    • @rell7725 你也在使用双斜杠,即使你应该只使用单斜杠
    • 将斜线从双斜线改为单斜线
    • @rell7725 你能用更新的代码和错误更新你的帖子吗?
    • @rell7725 对路径进行更正后,我无法重现您的问题,因为文件移动对我来说很好。您确定指定目录中存在“Testing doc 2.pdf”文件吗?
    • 当我回到源文件夹时才意识到文件名是“Testing doc 2”而不是.pdf。它只是一个pdf文件。但是,我仍然收到错误消息。使用代码和错误更新表单
    猜你喜欢
    • 2021-11-24
    • 2014-06-08
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2022-12-22
    相关资源
    最近更新 更多