简体   繁体   中英

Python not appending to .txt file

I have a strange issue i thought was easy enough, what i'm essentially trying to do is append text to an already created .txt file, but in my tests it is always over writing the data.

Code:

def verify_mac_codes_working(self, macro_code, the_url, mode):
    macro_folder = "C:\\Users\\Graham\\Desktop\\Files\\programming\\PaydayDreamsProgramming\\Python\\rank-jester\\rank-jester-capp-macro\\bin\\Debug\\Macros\\"
    the_root_url = self.root_url(the_url)
    # create the .txt file ...
    with open(macro_folder + the_root_url + ".txt", 'w') as file_reg:
        if mode == "mode_register":
            for code in macro_code:
                file_reg.write("%s\n" % code)

    # append data to the .txt file ...
    if mode == "mode_login_and_post":
        with open(macro_folder + the_root_url + ".txt", 'a+') as file_lap:
            for code in macro_code:
                file_lap.write("%s\n" % code)
        with open(macro_folder + the_root_url + ".txt", 'a+') as append_file:
            append_file.write("--> " + self.root_url(the_url) + "\n--> article_rank_jester" + "\n--> Creates a profile page with html link ...")

I have tried using " a " aswell as " a+ " in each test it over writes the data in the mode_login_and_post i cannot see the issue, any help would be appreciated.

As per Python documentation on input and output , using mode w when opening a file will cause the existing content to be erased:

The second argument is another string containing a few characters describing the way in which the file will be used. mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end.

You might also want to consider to use print instead of write .

Every time you call your function verify_mac_codes_working you execute the initialisation that always creates a new file as you use mode w when opening the file (and therefore the remainder of your code appends to an empty file):

with open(macro_folder + the_root_url + ".txt", 'w') as file_reg:
    if mode == "mode_register":
        for code in macro_code:
            file_reg.write("%s\n" % code)

It'd be better to first check if the file already exists, and if it does, to skip this part of the code. In order to check if a file exists, you could use the os library:

import os.path
if not os.path.exists(path):
    # add your file creation code here

Alternatively you could flip your code around:

if mode == "mode_register":
    with open(macro_folder + the_root_url + ".txt", 'w') as file_reg:
        for code in macro_code:
            file_reg.write("%s\n" % code)

This way, you check the mode first (before opening a file). Note if mode == "mode_register" is true, this piece of code will overwrite/erase the file if it exists already.

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