請你寫出完整的代碼,這段代碼報錯Traceback (most recent call last): File "D:\pythonProject\venv\Lib\site-packages\pandas\io\common.py", line 25, in <module> f.write(t.read()) UnicodeDecodeError: 'cp950' codec can't decode byte 0xc8 in position 6678: illegal multibyte sequence,代碼如下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 文件的内容汇总到一个文档 with open('汇总文档.txt', 'w') as f: for file_name in os.listdir(dst_folder): if file_name.endswith('.txt'): with open(os.path.join(dst_folder, file_name), 'r') as t: f.write(t.read())
时间: 2023-12-12 20:04:04 浏览: 311
您的代码中涉及到读取文件时编码出错的问题,可以在打开文件时指定编码格式为 'utf-8' 试试看,以下是修改后的代码:
```python
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 文件的内容汇总到一个文档
with open('汇总文档.txt', 'w', encoding='utf-8') as f:
for file_name in os.listdir(dst_folder):
if file_name.endswith('.txt'):
with open(os.path.join(dst_folder, file_name), 'r', encoding='utf-8') as t:
f.write(t.read())
```
您可以尝试运行一下这个修改后的代码,看看是否还会出现报错。
阅读全文