我正在编写一个数据解析脚本,其中数据每小时刷新一次。我让脚本工作,它从第一次运行脚本时获取最近的文件。但如果在脚本当前运行时交付更新,则无法检测到新文件。当它在油灰中运行时,这不是一个问题,但我正在烧瓶中重新创建所有内容
def getLatestFile():
import glob, os
list_of_files = glob.glob('./Source/data.parsed*.txt')
latest_file = max(list_of_files, key=os.path.getctime)
return latest_file
if __name__ == '__main__':
###Creates a Dictionary from the latest Data File
headers = None
content = {}
csvFile = getLatestFile()
modTime = os.path.getmtime(csvFile)
reader=csv.reader(open(csvFile), delimiter = '|') #opens File
print('Creating Dictionary from file ' + csvFile + '\nLast modified date - ' + str(datetime.datetime.fromtimestamp(modTime)))
for row in reader: # Writes data to dictionary
if reader.line_num == 1:
headers = row[1:] #grabs first row and creates headers
print(headers)
else:
content[row[0]] = dict(zip(headers, row[1:])) #creates dictionary
app.run(host=os.getenv('IP', '0.0.0.0'), port =int(os.getenv('PORT', 8080)), debug=True)
当我尝试用下面的代码重新创建字典时,它只获取脚本创建时的最新文件
def updateDict():
headers = None
content = {}
csvFile = getLatestFile()
modTime = os.path.getmtime(csvFile)
for row in reader: # Writes data to dictionary
if reader.line_num == 1:
headers = row[1:] #grabs first row and creates headers
else:
content[row[0]] = dict(zip(headers, row[1:])) #creates dictionary
我尝试了latest_file = max(list_of_files, key=os.path.getmtime)
,但是它仍然忽略掉掉到源目录中的新文件
os.path.getctime()
的工作方式很奇怪:因此,最好检测最后创建了哪个文件,但不要检测是否已经修改了现有文件
我建议更改密钥以计算创建时间和修改时间之间的最大值:
相关问题 更多 >
编程相关推荐