File size: 734 Bytes
5077cab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import time
import os
def clean_file_content():
BASE_DATA = "/data"
LOG_FILE = os.path.join(BASE_DATA, "success_results.txt")
print('3分钟后清理文件内容,保留第一行')
time.sleep(180)
try:
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
with open(LOG_FILE, "r", encoding="utf-8") as file:
lines = file.readlines()
first_line = lines[0] if lines else ""
with open(LOG_FILE, "w", encoding="utf-8") as file:
file.write(first_line)
print("文件内容已清理,只保留第一行")
except Exception as e:
print(f"清理文件时出错: {e}")
if __name__ == "__main__":
while True:
clean_file_content()
|