问题描述
我正在尝试使用以下代码查看文件是否存在。问题在于文件中有一个数字会随着文件的生成而改变,而我不知道如何与更改名称的文件相匹配。 文件名为FD 464738-DailyReport.xlsx现有代码:
#Match and find if file exists
while not os.path.exists('/home/jobsuser/Downloads/FD 464738 - DailyReport.xlsx'):
time.sleep(1)
#Move and rename file
shutil.move('/home/jobsuser/Downloads/FD 464738 - DailyReport.xlsx','/home/jobsuser/files/Dailyfdreport_'+fnameyesterday+'.xlsx')
我尝试用通配符*替换数字部分,但该部分无效。 寻找任何可以使比赛进行的建议。
解决方法
您可以尝试这样的事情:
from pathlib import Path
path = Path('/home/jobsuser/Downloads')
while list(path.glob('FD * - DailyReport.xlsx')) == []:
time.sleep(1)
files = list(path.glob('FD * - DailyReport.xlsx'))
shutil.move(files[0],...)
有一些未知数,因此肯定还有改进的空间。