Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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_File_Io - Fatal编程技术网

在python中写入文本文件的最后一行不起作用

在python中写入文本文件的最后一行不起作用,python,file,io,Python,File,Io,我不知道为什么,但是有了下面的代码,一切正常,所有的文本都被取出,然后放回文本文件 def upgradecap(): yc = open("ycfile", 'r') a = yc.readline() b = yc.readline() c = yc.readline() d = yc.readline() e = yc.readline() f = yc.readline() g = yc.readline() h = yc.readline() i = yc.readline() j =

我不知道为什么,但是有了下面的代码,一切正常,所有的文本都被取出,然后放回文本文件

def upgradecap():
yc = open("ycfile", 'r')
a = yc.readline()
b = yc.readline()
c = yc.readline()
d = yc.readline()
e = yc.readline()
f = yc.readline()
g = yc.readline()
h = yc.readline()
i = yc.readline()
j = yc.readline()
k = yc.readline()
cap = yc.readline()
cap = int(cap)
cap = cap + 2500
cap = str(cap)
l = yc.readline()
yc = open("ycfile", "w+")
yc.write(a)
yc.write(b)
yc.write(c)
yc.write(d)
yc.write(e)
yc.write(f)
yc.write(g)
yc.write(h)
yc.write(i)
yc.write(j)
yc.write(k)
yc.write(cap + '\n')
yc.write(l)
yc.close()
L62.configure(text=cap)
但是下一行代码将所有内容都写回文件,除了在第二个函数中写入文件的最后一行之外

def upgradetrn():
yc = open("ycfile", 'r')
a = yc.readline()
b = yc.readline()
c = yc.readline()
d = yc.readline()
e = yc.readline()
f = yc.readline()
g = yc.readline()
h = yc.readline()
i = yc.readline()
j = yc.readline()
trn = yc.readline()
trn = int(trn)
trn = trn + 1
trn = str(trn)
k = yc.readline()
x = yc.readline()
yc = open("ycfile", "w+")
yc.write(a)
yc.write(b)
yc.write(c)
yc.write(d)
yc.write(e)
yc.write(f)
yc.write(g)
yc.write(h)
yc.write(i)
yc.write(j)
yc.write(trn + '\n')
yc.write(k)
yc.write(x)
yc.close()
L61.configure(text=trn)
我所要做的就是从文本文件中取出每一行,编辑一行,然后全部放回。
有人知道为什么会这样吗?谢谢你的回答

两件事第一个,问题。
据我所知,除了最后一个
write()
调用之外,其他所有调用都没有写入文件?
这是因为当您以
'w'
'w+'
模式写入文件时,该文件中的所有内容都会被您正在写入的内容替换。
因此,如果我有一个包含单词
'dog'
的文件,然后执行以下操作:

file.write('cat')
file.write('goldfish')
“狗”将替换为“猫”,然后“猫”将替换为“金鱼”。所以你只剩下“金鱼”了。
要解决此问题,请在文件上使用
'a'
(追加)模式

file = open('ycfile', 'a')
现在,无论何时调用
write()
,它都会将新文本添加到文件中,而不是覆盖它。
我已经包括了这一点,这样你就可以了解出了什么问题,这样你就知道如果你将来遇到它,如何解决它。然而,有一个更好的方法来解决这个问题

第二个,您的代码。
你要做的不是一行一行地处理文件,而是获取文件的所有文本,更改所需的位,然后用新文本替换文件文本。
也许它看起来像这样:

def upgradeTrn():

    readfile = open('ycfile.txt', 'r+')
    text = readfile.read()
    lines = text.split('\n') # split the file content by line

    data = lines[10] #target the desired line
    trn = str(int(data) + 1)
    lines[10] = trn #replace the line with the new content

    new_text = '\n'.join(lines)
    readfile.write(new_text)
    readfile.close()

阅读更多关于附加模式的信息,以防您感兴趣

编码是最后一件事。当你必须写十遍同样的东西时,首先思考和设计。。。你可能想要一个循环,这个问题与tkinter有什么关系?谢谢你的建议,我现在把代码缩短了,它可以工作了,我确实在我写的每一段代码之后都有\n,但这留下了空白行,所以我把它取出来,出于某种原因,它把每一段代码都放在一行上,这正是我想要的