好吧,所以我下午的大部分时间都在调试这个,我认输了。
我正在尝试创建一个系统,该系统将在我的学生工作人员完成工作后帮助自动将工作文件夹放回服务器上。到目前为止,我已经完成了所有工作——它正确识别了匹配的服务器文件夹,并且能够将服务器文件夹移动到存档文件夹中,以便在我将新作品复制回来时临时备份它。
然而,正是最后一点阻碍了我。一切都运行良好,直到我将作为参数传递的文件夹复制回服务器的部分。
这是有问题的代码:
print "Match Source: " + match_src
archive_folder_name = match_src.rsplit('/', 1)[1]
print "Archive Folder Name: " + archive_folder_name
if not (os.path.isdir(os.path.join(os.path.dirname(match_src), "Archive"))):
os.mkdir(os.path.join(os.path.dirname(match_src), "Archive"))
archive_dst = os.path.join(os.path.join(os.path.dirname(match_src), "Archive"), archive_folder_name)
print "Archived Folder Destination: " + archive_dst
# okay, archive folder has been made. Now we need to move the old
# stuff to this folder.
shutil.move(match_src, archive_dst)
# okay, archive folder is filled. Now to move the new stuff there
print "Updated Source: " + updated_folder_path
print "Destination: " + os.path.dirname(match_src)
shutil.move(updated_folder_path, os.path.dirname(match_src))
这是这些打印语句的输出和错误代码:
ServerReturn mwl36$ python serverreturn_main.py /Users/mwl36/Desktop/Week\ 1
I'm on a Mac.
Path: /Users/mwl36/Desktop/Week 1
Folder: Week 1
Match Source: /Volumes/GCPSX Mac HD/ID/Marisa/Work for Student Workers/201315/SERVERTEST/Week 1
Archive Folder Name: Week 1
Archived Folder Destination: /Volumes/GCPSX Mac HD/ID/Marisa/Work for Student Workers/201315/SERVERTEST/Archive/Week 1
Updated Source: /Users/mwl36/Desktop/Week 1
Destination: /Volumes/GCPSX Mac HD/ID/Marisa/Work for Student Workers/201315/SERVERTEST
Traceback (most recent call last):
File "serverreturn_main.py", line 124, in <module>
main()
File "serverreturn_main.py", line 117, in main
shutil.move(updated_folder_path, os.path.dirname(match_src))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 296, in move
copytree(src, real_dst, symlinks=True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 206, in copytree
raise Error, errors
shutil.Error: [('/Users/mwl36/Desktop/Week 1/testfile.txt', '/Volumes/GCPSX Mac HD/ID/Marisa/Work for Student Workers/201315/SERVERTEST/Week 1/testfile.txt', "[Errno 22] Invalid argument: '/Volumes/GCPSX Mac HD/ID/Marisa/Work for Student Workers/201315/SERVERTEST/Week 1/testfile.txt'")]
我哪里错了?如果对 do shutil.move(src, dst) 的调用第一次没有问题,为什么会在这里呕吐?我尝试在初次通话后添加延迟,但没有效果。
编辑:更奇怪的是它实际上会执行移动,但似乎永远不会删除桌面上的旧文件夹。本质上,它将文件夹复制到 DST 位置,但不会自行清理。
编辑#2:调用 os.listdir 表明该目录已经消失,并且在第二次移动调用之前唯一的东西是“存档”。我明天上班时会测试 copy() 并再次访问代码。