简体   繁体   中英

File Transfer w/ python

I am trying to make a program that will: Copy & Paste a Directory and place it, and it's contents into a new location. I do not think my code is right for this, I think it is simple just moving the file to a totally different location

import os
import shutil
login = os.getlogin()

SOURCE_FILE_DEKSTOP =  '/Users/%s/Desktop' % (login)
DST_FILE_WD = 'Users/%s/WorkDocs' % (login)

shutil.move(SOURCE_FILE_DEKSTOP, DST_FILE_WD)

I am getting this error as well

Traceback (most recent call last):
   File "/Users/gomcrai/pythings/fileTransfer.py", line 8, in <module>
    shutil.move(SOURCE_FILE_DEKSTOP, DST_FILE_WD)
   File "/Library/Frameworks/Python.framework/Versions/2.7/lib    /python2.7/shutil.py", line 300, in move
rmtree(src)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 256, in rmtree
    onerror(os.rmdir, path, sys.exc_info())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 254, in rmtree
os.rmdir(path)
OSError: [Errno 13] Permission denied: '/Users/gomcrai/Desktop'

If you genuinely want to copy, rather than move, then shutil.copytree is a better bet. move will, well, move the entry, deleting the original.

You're getting an error because Macs have access control lists on the folders that it thinks should always be there, and Desktop is one of those, since it's used to store all the files visible on your desktop. Since shutil.move is trying to delete that, it's meeting the ACL and being denied.

ls -ale /Users/gomcrai/Desktop should show you the ACL, displaying something like: 0: group:everyone deny delete

If you'd like to cut and paste rather than copy and paste the directory, you could either use os.listdir or os.walk and find files and folders inside Desktop/ and move all of those, or use shutil.copytree on Desktop and then remove everything inside Desktop while leaving that folder alone.

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.

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