報錯如下,請修改代碼 File "D:\pythonProject\dianjian.py", line 14 if file_name.endswith('.txt'): ^ IndentationError: expected an indented block after 'for' statement on line 13完整代碼import os import shutil from datetime import date #指定目标文件夹路径和本地路径 src_folder = r"Y:\iplas\Robot_Log\SMT" dst_folder = "D:\測試log" #获取今天的日期 today = date.today() #遍历目标文件夹中的所有文件 for file_name in os.listdir(src_folder): if file_name.endswith('.txt'): file_path = os.path.join(src_folder, file_name) if date.fromtimestamp(os.path.getmtime(file_path)) == today: #如果是今天生成的 txt 文件,复制到本地路径 shutil.copy(file_path, os.path.join(dst_folder, file_name)) #将本地路径中所有 txt 文件的内容存储到列表中 file_contents = [] for file_name in os.listdir(dst_folder): if file_name.endswith('.txt'): with open(os.path.join(dst_folder, file_name), 'r', encoding='gbk') as t: file_contents.append(t.read()) #将列表中的内容写入到汇总文档中 with open(os.path.join(dst_folder, '汇总文档.txt'), 'w', encoding='gbk') as f: f.write('\n\n'.join(file_contents))
时间: 2023-12-12 18:03:29 浏览: 82
请在第13行后添加缩进,即将第14行缩进4个空格或1个制表符,如下所示:
```
for file_name in os.listdir(src_folder):
if file_name.endswith('.txt'):
file_path = os.path.join(src_folder, file_name)
if date.fromtimestamp(os.path.getmtime(file_path)) == today:
shutil.copy(file_path, os.path.join(dst_folder, file_name))
file_contents = []
for file_name in os.listdir(dst_folder):
if file_name.endswith('.txt'):
with open(os.path.join(dst_folder, file_name), 'r', encoding='gbk') as t:
file_contents.append(t.read())
with open(os.path.join(dst_folder, '汇总文档.txt'), 'w', encoding='gbk') as f:
f.write('\n\n'.join(file_contents))
```
阅读全文