读写后,把读写文件发生的异常信息写入到D:\2023training\log\training.log文件中。读写完成后,将D:\2023training\file02.txt文件删除
时间: 2023-08-23 10:05:22 浏览: 46
好的,我会为您更新代码以记录异常信息和删除文件。
```python
import logging
import os
file_path = r'D:\2023training\file02.txt'
copy_file_path = r'D:\2023training\file02_copy.txt'
log_file_path = r'D:\2023training\log\training.log'
# 设置日志配置
logging.basicConfig(filename=log_file_path, level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
try:
with open(file_path, 'r') as file:
content = file.read()
with open(copy_file_path, 'w') as copy_file:
copy_file.write(content)
os.remove(file_path)
print("文件复制完成,原始文件已删除。")
except Exception as e:
logging.exception("发生异常:")
print("文件复制过程中发生异常,请查看日志文件。")
```
这段代码会将读写过程中发生的异常信息记录在 `D:\2023training\log\training.log` 文件中,并在读写完成后删除原始文件 `D:\2023training\file02.txt`。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)