Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python如何在不删除内容的情况下继续写入文件';已经有了_Python_Python 3.x_File Io_Windows 7_Python 3.3 - Fatal编程技术网

Python如何在不删除内容的情况下继续写入文件';已经有了

Python如何在不删除内容的情况下继续写入文件';已经有了,python,python-3.x,file-io,windows-7,python-3.3,Python,Python 3.x,File Io,Windows 7,Python 3.3,在Windows中编写python 3.3程序时,我遇到了一个小问题。我正试图将一些指令行写入一个文件,以便程序执行。但每次我在下一行输入file.write()时,它都会替换上一行。我希望能够继续将尽可能多的行写入此文件。注意:使用“\n”似乎不起作用,因为您不知道将有多少行。请帮忙!以下是我的代码(作为一个循环,我确实运行了多次): 每次打开要写入的文件时,它都会被删除(截断)。打开文件进行附加,或者只打开一次并保持打开状态 要打开要追加的文件,请对模式使用a而不是w: while menu

在Windows中编写python 3.3程序时,我遇到了一个小问题。我正试图将一些指令行写入一个文件,以便程序执行。但每次我在下一行输入file.write()时,它都会替换上一行。我希望能够继续将尽可能多的行写入此文件。注意:使用“\n”似乎不起作用,因为您不知道将有多少行。请帮忙!以下是我的代码(作为一个循环,我确实运行了多次):


每次打开要写入的文件时,它都会被删除(截断)。打开文件进行附加,或者只打开一次并保持打开状态

要打开要追加的文件,请对模式使用
a
而不是
w

while menu != None:
    menu = lipgui.choicebox("Select an option:", choices=["choice1", "choice2", "choice3"])
    file = open("file.txt", "a")
    if menu == "choice1":
       text_to_write = lipgui.enterbox("Text to write:")
       file.write(text_to_write)
或者在循环外部打开文件:

或者在您第一次需要它的时候只需要一次:

file = None

while menu != None:
    menu = lipgui.choicebox("Select an option:", choices=["choice1", "choice2", "choice3"])
    if file is None:
        file = open("file.txt", "w")
    if menu == "choice1":
       text_to_write = lipgui.enterbox("Text to write:")
       file.write(text_to_write)

为了解决这类问题并处理包括实时应用程序在内的所有应用程序,您必须首先以附加模式在外部循环中打开一个文件 并在循环内以写模式打开相同的文件

下面是不同上下文中的示例(实时删除重复项)


但我不会重新打开文件。我又给它写信了@卢凯丁克勒:那就给我们看看你的密码;如果行被替换,那么您正在做一些不寻常的事情。@LukeDinkler:您正在重新打开文件。每次调用
open()
。噢!愚蠢的错误!现在,我如何将每一行写入新行?@LukeDinkler:在您编写的文本中添加一个新行字符。此外,由于它是python 3.3,您应该将open('file.txt','w')用作文件,而不是直接使用open@user3012759这个特性是在Python 2.5中引入的
file = open("file.txt", "w")

while menu != None:
    menu = lipgui.choicebox("Select an option:", choices=["choice1", "choice2", "choice3"])
    if menu == "choice1":
       text_to_write = lipgui.enterbox("Text to write:")
       file.write(text_to_write)
file = None

while menu != None:
    menu = lipgui.choicebox("Select an option:", choices=["choice1", "choice2", "choice3"])
    if file is None:
        file = open("file.txt", "w")
    if menu == "choice1":
       text_to_write = lipgui.enterbox("Text to write:")
       file.write(text_to_write)
import sched, time
s = sched.scheduler(time.time, time.sleep)
outfile = open('D:/RemoveDup.txt', "a")
def do_something(sc):  #loop  
    outfile = open('D:/RemoveDup.txt', "w")
    #do your stuff........    
    infile = open('D:/test.txt', "r")
    lines_seen = set()
    for line in infile:
        if line not in lines_seen:
            outfile.write(line)
            lines_seen.add(line)
    s.enter(10, 1, do_something, (sc,))
    s.enter(10, 1, do_something, (s,))
s.run()