Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reopen file for appending

There is something I'm not understanding about file output. I want to open a file, write to it, close it, then reopen it, append, close.

I don't want keep the file open the entire time the script is running.

All I ever see in the file is the * Start *. I want to see the fail messages and the Complete message. How do I do this?

log_failed_download_file = open(log_failed_download_filename, "w")
log_failed_download_file.write ("\n*** Start ***");
log_failed_download_file.close()

# other logic (os.chdir)

for x in range(start_x, end_x + 1): 
    # do stuff, possibly set download_error to False
    if (download_error == False):
        log_failed_download_file = open(log_failed_download_filename, "a")
        log_failed_download_file .write(url)
        log_failed_download_file .close()

# other logic

log_failed_download_file = open(log_failed_download_filename, "a")
log_failed_download_file.write ("\n\nComplete - %r" % str(datetime.datetime.now().strftime('%m/%d/%Y  %H:%M:%S')))
log_failed_download_file.close()

Edit: I've added that os.chdir is in the "other logic" part because that is the source of the problem.

like image 797
Al Lelopath Avatar asked Mar 31 '26 16:03

Al Lelopath


1 Answers

If log_failed_download_filename is just the name of the file, changing the current working directory will change where you write to. So you are getting half the log written to one place, then the rest written to another after you call os.chdir.

To avoid this, you can make your filename into the whole filepath:

log_path = os.path.abspath(log_failed_download_filename)

This gives the absolute path to the file. From the documentation for os.path.abspath:

Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).

Once you call os.chdir, os.getcwd could return something different, so abspath would also be different.

Just to note, modern Python opens files using the with context manager. This automatically closes files for you:

with open(log_path, 'w') as log:
    log.write("\n*** Start ***")

# other logic (os.chdir)

for x in range(start_x, end_x + 1): 
    # do stuff, possibly set download_error to False
    if not download_error:
        with open(log_path, 'a') as log:
            log.write(url)

# other logic

with open(log_path, 'a') as log:
    log.write("\n\nComplete - %r" %
               str(datetime.datetime.now().strftime('%m/%d/%Y  %H:%M:%S')))
like image 114
Peter Wood Avatar answered Apr 03 '26 04:04

Peter Wood