我每天有一些 Pandas 代码运行 9 个不同的文件。目前,我有一个计划任务在某个时间运行代码,但有时我们的客户端没有按时将文件上传到 SFTP,这意味着代码将失败。我想创建一个文件检查脚本。
The*_*ake 10
缩短法尔汉的答案。您可以使用列表理解并特别简化代码。
import os, time
while True:
filelist = ['file1', 'file2', 'file3']
if all([os.path.isfile(f) for f in filelist]):
break
else:
time.sleep(600)
Run Code Online (Sandbox Code Playgroud)
import os, time
filelist = ['file1','file2','file3']
while True:
list1 = []
for file in filelist:
list1.append(os.path.isfile(file))
if all(list1):
# All elements are True. Therefore all the files exist. Run %run commands
break
else:
# At least one element is False. Therefore not all the files exist. Run FTP commands again
time.sleep(600) # wait 10 minutes before checking again
Run Code Online (Sandbox Code Playgroud)
all() 检查列表中的所有元素是否都是True. 如果至少有一个元素,False则返回False。
另一种更简单的方法使用map:
import os
file_names_list = ['file1', 'file2', 'file3']
if all(list(map(os.path.isfile,file_names_list))):
# do something
else:
# do something else!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6081 次 |
| 最近记录: |