[英]How to accurately check folders or files in python
I've been trying to code something that helps me make a file whenever it got deleted.我一直在尝试编写一些代码,以便在文件被删除时帮助我创建文件。
My code:我的代码:
import os
from os.path import exists
from time import time, sleep
#Variable
check_folder = exists(#path)
def checkfilepy():
if check_folder:
print('ClientSettings have already existed!')
else:
create_folder = os.makedirs(#path)
print('Successfully created ClientSettings!')
#Variable
check_file = exists(#path)
#If statement
if check_file:
print('file have already existed!')
else:
create_file = open(#path, 'x')
create_file.write(#write stuff in)
print('Successfully created file')
while True:
sleep(60 - time() % 60)
cfp = checkfilepy()
Do you guys have any ideas to continuously check if the file is still there?你们有什么想法可以不断检查文件是否仍然存在吗?
Like Sayse, I would prefer to only check when needed.和 Sayse 一样,我更愿意只在需要时检查。 But if you *really* want to check regularly, you can use the
threading
library:但是如果你*真的*想定期检查,你可以使用
threading
库:
import threading
import time
def frequent_check():
while True:
checkfilepy()
time.sleep(60 - time.time() % 60)
if __name__ == '__main__':
t = threading.Thread(target=frequent_check)
t.run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.