简体   繁体   English

os.rename()在Python中的时间

[英]os.rename() time in Python

I'm building a script that renames some files to determine if the file is locked or not. 我正在构建一个重命名某些文件的脚本,以确定文件是否被锁定。 The script does the following on every file in a given directory recursively. 该脚本以递归方式对给定目录中的每个文件执行以下操作。

try: 
    os.rename(source, temp)
    os.rename(temp, source)
except OSError as e:
    print 'exception'

My question is should there be a time.sleep(1) between the os.rename() calls? 我的问题是os.rename()调用之间应该有一个time.sleep(1)吗? I'm worried the file might not be renamed by the time the other os.rename() call takes place, but I want it to run as fast as possible. 我担心在其他os.rename()调用发生时可能无法重命名该文件,但我希望它能尽快运行。

Thanks for any help. 谢谢你的帮助。

否,因为这些命令都被阻止了。

The second os.rename() will be call only when the first will have finished (principle of blocking command). 仅当第一个os.rename()完成时才调用第二个os.rename() (阻塞命令的原理)。 So you don't have to worry about the time of execution. 因此,您不必担心执行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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