Python 3.x python3使用os.path.exists和shutil.move检查和移动使用通配符或与数字匹配

Python 3.x python3使用os.path.exists和shutil.move检查和移动使用通配符或与数字匹配,python-3.x,operating-system,matching,shutil,Python 3.x,Operating System,Matching,Shutil,我正在尝试使用下面的代码查看是否存在文件。问题是文件中有一个数字在生成文件时会发生变化,我不知道如何与名称发生变化的文件进行匹配。 文件名为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 renam

我正在尝试使用下面的代码查看是否存在文件。问题是文件中有一个数字在生成文件时会发生变化,我不知道如何与名称发生变化的文件进行匹配。 文件名为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], ...)

还有一些未知数,因此肯定还有改进的余地。

这对我来说非常有效。我必须通过将文件强制转换为shutil中的字符串来解决路径问题。move:AttributeError:'PosixPath'对象没有属性'rstrip'