請你幫我加一行代碼,我將匯總的TXT文件再複製到另外的路徑import os import shutil from datetime import date import time #指定目标文件夹路径和本地路径 src_folder = r"Y:\iplas\Robot_Log\SMT" dst_folder = "D:\測試log" start_time = time.time() #获取今天的日期 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: 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)) #获取程序结束时间,计算程序运行时间 end_time = time.time() run_time = end_time - start_time print(f"程序运行时间:{run_time:.2f} 秒")
时间: 2023-12-12 12:05:46 浏览: 77
這裡是代碼,可以將匯總的TXT文件複製到另一個文件夾中後,再將其複製到指定路徑下:
```python
import os
import shutil
from datetime import date
import time
#指定目标文件夹路径和本地路径
src_folder = r"Y:\iplas\Robot_Log\SMT"
dst_folder = "D:\測試log"
temp_folder = "D:\暫存log"
start_time = time.time()
#获取今天的日期
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:
shutil.copy(file_path, os.path.join(temp_folder, file_name))
#遍历本地路径中的所有文件
file_contents = []
for file_name in os.listdir(temp_folder):
if file_name.endswith('.txt'):
with open(os.path.join(temp_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))
#將匯總的TXT文件再複製到另外的路徑
for file_name in os.listdir(temp_folder):
if file_name.endswith('.txt'):
shutil.copy(os.path.join(temp_folder, file_name), os.path.join(dst_folder, file_name))
#刪除暫存文件夾
shutil.rmtree(temp_folder)
#获取程序结束时间,计算程序运行时间
end_time = time.time()
run_time = end_time - start_time
print(f"程序运行时间:{run_time:.2f} 秒")
```
這個代碼將匯總的TXT文件複製到 "D:\測試log" 下後,再將其複製到指定路徑下,並且在完成後刪除了暫存文件夾。
阅读全文