My goal is to take the contents of all text files in subfolders created today and move them to a single existing report.txt but I can't seem to find a good way to go about it. I'm not very experienced in coding so any help would be much appreciated. Here is what I have so far (I know it's rubbish):
if getmtime == today:
with open(glob.iglob(drive + "://CADIQ//CADIQ_JOBS//?????????????????????")) as f:
for line in f:
content += line
with open(reportFile, "a") as f:
f.write(content)
我首先创建一个
desired_date
对象,它是一个datetime.date
。然后,您可以将该日期格式化为一个字符串,该字符串构成您希望在glob中查找的模式。全球模式不在乎时间,只在乎日期从那里,您可以访问每个路径,对当前路径中的所有文本文件进行全局搜索,并累积每个文本文件中的行。最后,将所有内容写入一个文件
试试这个,基于How do I list all files of a directory?
希望这有帮助:) 最好尝试以字节为单位读取或写入文件,因为有时可能会损坏数据
相关问题 更多 >
编程相关推荐