python实现:判断D盘下b文件夹下文件a是否有新内容写入
时间: 2024-05-07 16:15:11 浏览: 138
可以使用Python的os和time模块来实现判断D盘下b文件夹下文件a是否有新内容写入的功能。具体实现步骤如下:
1. 导入os和time模块。
```python
import os
import time
```
2. 获取文件a的路径。
```python
file_path = r'D:\b\a.txt'
```
3. 获取文件a的最后修改时间。
```python
last_modified_time = os.path.getmtime(file_path)
```
4. 进行轮询,每隔一定时间检查一次文件a是否有新内容写入。
```python
while True:
current_modified_time = os.path.getmtime(file_path)
if current_modified_time != last_modified_time:
print('文件a有新内容写入。')
last_modified_time = current_modified_time
time.sleep(5) # 每隔5秒检查一次文件a是否有新内容写入
```
完整代码如下:
```python
import os
import time
file_path = r'D:\b\a.txt'
last_modified_time = os.path.getmtime(file_path)
while True:
current_modified_time = os.path.getmtime(file_path)
if current_modified_time != last_modified_time:
print('文件a有新内容写入。')
last_modified_time = current_modified_time
time.sleep(5) # 每隔5秒检查一次文件a是否有新内容写入
```
阅读全文
相关推荐

















