ฉันได้สร้างสคริปต์ python เพื่ออ่านจากไฟล์ที่อัปเดตอย่างต่อเนื่อง ('out.txt') และเขียนไปยังไฟล์อื่น ('received.txt') ทุกๆ 10 วินาที ตอนนี้ฉันต้องหาวิธีลบข้อมูลที่อ่านแล้วออกจากไฟล์ 'out.txt' นี่คือรหัสที่ฉันมีจนถึงตอนนี้
#!/usr/bin/python
import sys
import time
num_lines = sum(1 for line in open('out.txt')) #find the last line
print num_lines
sys.stdout = open('received.txt', 'w') #write to the received.txt file
print
f = open('out.txt', 'r') #open ‘out.txt’ with read permissions
f.readline(num_lines) #read the last line of ’out.txt’
while True: #start loop to print remaining lines in out.txt
for line in f:
print line
time.sleep(10) #sleep for 10 seconds
ฉันจะลบข้อมูลใน 'out.txt' หลังการวนซ้ำหรือภายในการวนซ้ำหรือไม่ ฉันควรใช้ f.write เพื่อสิ่งนี้หรือไม่ ฉันใช้ Raspbian กับ Raspberry Pi เพื่อสิ่งนี้ ข้อมูลสำหรับ 'out.txt' ดูเหมือนว่า
ไอบีคอนสแกน ...
3F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1 -71 -66
3F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1 -71 -66
3F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1 -71 -66
... อัปเดตอย่างต่อเนื่อง
คำแนะนำใด ๆ จะเป็นประโยชน์อย่างยิ่ง ขอบคุณ!
w+
) ไฟล์จะตัดทอน (ลบเนื้อหาทั้งหมด) ของไฟล์ - person okoboko   schedule 31.07.2014