請你幫我修改,只要其中有一行只要有一個日期非今天的日期就將其行進行刪除import datetime today_str = datetime.datetime.now().strftime('%Y/%m/%d') with open('D:\手臂log.txt', 'r') as f: lines = f.readlines() keep_lines = [] for line in lines: if today_str in line: keep_lines.append(line) with open('D:\手臂log111.txt', 'w') as f: for line in keep_lines: if today_str not in line: continue f.write(line)
时间: 2024-02-23 18:58:07 浏览: 101
python实现将中文日期转换为数字日期
以下是修改后的代码:
```python
import datetime
today_str = datetime.datetime.now().strftime('%Y/%m/%d')
with open('D:\手臂log.txt', 'r') as f:
lines = f.readlines()
keep_lines = []
for line in lines:
if today_str not in line:
continue
keep_lines.append(line)
with open('D:\手臂log111.txt', 'w') as f:
for line in keep_lines:
f.write(line)
```
修改后的代码会判断每一行是否包含今天的日期,只有当所有日期都是今天的日期时,才会将该行写回文件中。如果某一行日期不是今天的日期,则会跳过该行,不将其写回文件中。
阅读全文