I'm attempting to write a script to move a file from one location to another locally on a machine. I am writing this script for Mac OS.
Code:
#!/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'
Updated code:
#!/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)
Update 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'
Your paths should not include the 'Macintosh HD' instead it should just start with /Users
Edit : Also, change the double slashes in the path to single slashes
Make sure you have installed the shutil module properly. You can use the below command.
pip3 install pytest-shutil
Then use the following code.
import shutil
shutil.move(r"/Users/jerel/Desktop/Testing/Testing doc 2.pdf", r"/Users/jerel/Desktop")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.