查找文本后面的变量和所有字符



我有一个代码,它查找文件并获取上次保存(修改)的时间,然后将其与新时间进行比较。如果发现差异,它将弹出一个通知。

但是电子表格March.xlsx名称可以更改为(示例)电子表格March 123.xlsx,我正在寻找类似于excel的东西,在末尾添加*,因此它将包括电子表格路径变量后面的所有内容。

import os.path, time
from win10toast import ToastNotifier

SpreadsheetPath = "XSpreadsheet March.xlsx"
Spreadsheet_OldTime = time.ctime(os.path.getmtime(SpreadsheetPath))
def notif(file):
toaster = ToastNotifier()
toaster.show_toast(file, file + " has been saved")
while True:
print("Comparing Spreadsheet Time - " + Spreadsheet_OldTime[11:19] + " and " + time.ctime(os.path.getmtime(SpreadsheetPath))[11:19])
if Spreadsheet_OldTime != time.ctime(os.path.getmtime(SpreadsheetPath)):
notif("Spreadsheet")
Spreadsheet_OldTime = time.ctime(os.path.getmtime(SpreadsheetPath))
print("Notification Pushed")
else:
print("No difference found...")
print("Sleeping for 10 secondsn")
time.sleep(10)

我试着谷歌一下,但不幸的是找不到一个简单易行的解决方案

您可以使用glob来查找与您提供的模式匹配的路径名。从库页:

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']

谢谢。我使用了glob

import glob
import time
import os.path
path = glob.glob('W:Spreadsheet March*')
x = time.ctime(os.path.getmtime(*path))
print(x)

最新更新