简体   繁体   English

Python 未附加到 .txt 文件

[英]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.我有一个奇怪的问题,我认为这很简单,我基本上想做的是将文本附加到已创建的 .txt 文件中,但在我的测试中,它总是覆盖数据。

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.我曾尝试在每次测试中使用“ a ”和“ a+ ”,它会覆盖mode_login_and_post 中的数据,我看不到问题,任何帮助将不胜感激。

As per Python documentation on input and output , using mode w when opening a file will cause the existing content to be erased:根据有关 input 和 output 的 Python 文档,在打开文件时使用模式w将导致现有内容被删除:

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; mode 可以是 'r' 只读取文件,'w' 只写入(现有的同名文件将被删除),'a' 打开文件进行追加; any data written to the file is automatically added to the end.写入文件的任何数据都会自动添加到末尾。

You might also want to consider to use print instead of write .您可能还需要考虑使用print而不是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):每次调用函数verify_mac_codes_working都会执行初始化,当打开文件时使用模式w时,它总是会创建一个新文件(因此,代码的其余部分会附加到一个空文件中):

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:为了检查文件是否存在,您可以使用os库:

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.请注意,如果mode == "mode_register"为真,这段代码将覆盖/擦除已经存在的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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