简体   繁体   中英

Python - How do you write a new line in a text file for ever iteration of a while loop?

I am trying to create a log file that captures the output of every iteration of a while loop, however I only seem to be able to capture the penultimate line and a create a blank new line under that.

I have this code:

from glob import glob
import task_prep
import time
import datetime

path = glob('F:\\Transcoder\\staging\\prep\\*.xml')


while len(path) >= 0:
    log = open('log_file.txt', 'w')
    if int(len(path)):
        data = (str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) + ': files')
        print(data)
        log.write(data + '\n')
        task_prep.task_prep()
        path = glob('F:\\Transcoder\\staging\\prep\\*.xml')
        time.sleep(3)

    else:
        data = (str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) + ': no files')
        print(data)
        log.write(data + '\n')
        path = glob('F:\\Transcoder\\staging\\prep\\*.xml')
        time.sleep(3)

This is to just overwrite everything that's being written on the first line, however this is what gets printed:

2016-12-06 10:25:56: no files
2016-12-06 10:25:59: no files
2016-12-06 10:26:02: no files
2016-12-06 10:26:05: no files
2016-12-06 10:26:08: no files
2016-12-06 10:26:11: no files
2016-12-06 10:26:14: no files
2016-12-06 10:26:17: no files
2016-12-06 10:26:20: no files
2016-12-06 10:26:23: no files
2016-12-06 10:26:26: no files

Process finished with exit code 1

this is what gets written in the text file:

2016-12-06 10:26:23: no files

A string on line 1 and a new line underneath.

What am I doing wrong?

You are opening the file at each loop turn, at the position 0 in the file, hence overwriting the previous result.

You have to open your file before the while block to keep the previous position in memory:

from glob import glob
import task_prep
import time
import datetime

path = glob('F:\\Transcoder\\staging\\prep\\*.xml')

with open('log_file.txt', 'w') as log:
    while len(path) >= 0:
        if int(len(path)):
            data = (str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) + ': files')
            log.write(data + '\n')
            task_prep.task_prep()
            path = glob('F:\\Transcoder\\staging\\prep\\*.xml')
            time.sleep(3)

        else:
            data = (str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) + ': no files')
            log.write(data + '\n')
            path = glob('F:\\Transcoder\\staging\\prep\\*.xml')
            time.sleep(3)

Bonus : Here, I use with which makes the use of the context manager implemented in open . The file will automatically be closed after the end of the with block.

You could also use the 'a' mode (for append) but you will still open the file many times which is very inefficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM