[英]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. 如果您真正想复制而不是移动,那么shutil.copytree是一个更好的选择。
move
will, well, move the entry, deleting the original. move
将移动条目,并删除原始条目。
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. 由于Mac在其认为应该始终存在的文件夹上具有访问控制列表,而
Desktop
是其中之一,因为它用于存储台式机上所有可见的文件,因此您会收到错误消息。 Since shutil.move
is trying to delete that, it's meeting the ACL and being denied. 由于
shutil.move
尝试删除它,因此它符合ACL并且被拒绝。
ls -ale /Users/gomcrai/Desktop
should show you the ACL, displaying something like: 0: group:everyone deny delete
ls -ale /Users/gomcrai/Desktop
应该显示ACL,显示如下: 0: group:everyone deny delete
ls -ale /Users/gomcrai/Desktop
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. 如果您想剪切粘贴而不是复制和粘贴目录,则可以使用
os.listdir
或os.walk
在Desktop/
查找文件和文件夹,然后将其全部移动,或者在Desktop
和Desktop
上使用shutil.copytree
然后删除Desktop
内的所有内容,同时不保留该文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.