[英]Python shutil.move: odd softlinking
A script I wrote was meant to move individual day directories from an old location to a new location with a structure like this: 我编写的脚本旨在将单个日间目录从旧位置移动到具有以下结构的新位置:
/old/YYYY/MM/DD
/new/YYYY/MM/DD
and for another task (unrelated to the moving of data) I created a softlink in the new location like this (this was the first mistake I made): 对于另一个任务(与数据的移动无关),我在新位置中创建了这样的软链接(这是我犯的第一个错误):
/new/2011/09 -> /old/2011/09
My script essential used this function call: 我的脚本必须使用此函数调用:
for d in os.listdir("/old/2011/09"):
shutil.move(os.path.join("/old/2011/09/", d), os.path.join("/new/2011/09", d))
After running my script 2011/09 was empty in both. 运行我的脚本后,2011/09的内容均为空。 I had this occur at work with unarchived data...big problem.
我在使用未存档的数据时发生这种情况...大问题。 My question is why didn't shutil.move() give me an error that the day directory I was moving already existed?
我的问题是,为什么shutil.move()没有给我一个错误,提示我正在移动的日期目录已经存在? Each day inside 09 should have been the same directory because of the softlink.
由于存在软链接,因此09内的每一天都应该是同一目录。
/new/2011/09/01 == /old/2011/09/01
Doesn't the shutil.move call check the src and dst before calling shutil.copy2? 在调用shutil.copy2之前,shutil.move调用不检查src和dst吗? From the docs: "The destination directory must not already exist."
从文档中:“目标目录必须不存在。” or is that only when it uses rename?
还是仅当它使用重命名时? AND if it makes a difference both old and new locations were glusterfs.
并且,如果这有所作为,那么旧位置和新位置都将成为glusterfs。
shutil.move Documentation shutil.move文档
shutil.copy Documentation shutil.copy文档
Thanks for any clarity you can provide. 感谢您的澄清。
EDIT/UPDATE : I submitted a question to the python-list asking why this behavior existed and asked if it should be changed ( list archive ). 编辑/更新 :我向python列表提交了一个问题,询问为什么存在此行为,并询问是否应更改( 列表归档 )。 They suggested I file a bug report.
他们建议我提交一个错误报告。 While doing the tests to submit the bug I found out that this has been fixed in Python 2.7.
在进行测试以提交错误时,我发现此问题已在Python 2.7中修复。 You can see the differences in the source in the move function declaration: Python 2.6 and Python 2.7 .
您可以在move函数声明中看到源代码之间的差异: Python 2.6和Python 2.7 。
This still does the move/rename but won't magically delete an entire directory. 仍然可以进行移动/重命名,但不会神奇地删除整个目录。
It comes down to these two lines in shutil.move : 它归结为shutil.move中的这两行:
copytree(src, real_dst, symlinks=True)
rmtree(src)
where src='old'
and real_dst='new/old'
. 其中
src='old'
和real_dst='new/old'
。 The copytree
command copies old
to the subdirectory new/old
. 该
copytree
命令复制old
的子目录new/old
。 That goes fine, although it may not be what you intended. 很好,尽管可能不是您想要的。
rmtree
removes the old
directory. rmtree
删除old
目录。 That's a problem, since new
is now a dangling symlink. 这是一个问题,因为
new
现在是悬而未决的符号链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.