Python 是否打开/写入删除txt文件内容?
我基本上是在尝试从文件中读取一个数字,将其转换为int,再向其中添加一个,然后将新的数字重写回该文件。但是,每次我打开.txt文件时运行此代码时,它都是空的。任何帮助都将不胜感激,谢谢!我是一个python新手Python 是否打开/写入删除txt文件内容?,python,python-3.x,Python,Python 3.x,我基本上是在尝试从文件中读取一个数字,将其转换为int,再向其中添加一个,然后将新的数字重写回该文件。但是,每次我打开.txt文件时运行此代码时,它都是空的。任何帮助都将不胜感激,谢谢!我是一个python新手 f=open('commentcount.txt','r') counts = f.readline() f.close counts1 = int(counts) counts1 = counts1 + 1 print(counts1) f2 = open('commentcount.
f=open('commentcount.txt','r')
counts = f.readline()
f.close
counts1 = int(counts)
counts1 = counts1 + 1
print(counts1)
f2 = open('commentcount.txt','w') <---(the file overwriting seems to happen here?)
f2.write(str(counts1))
f=open('commentcount.txt','r')
计数=f.读线()
f、 接近
counts1=int(计数)
counts1=counts1+1
打印(计数1)
f2=打开('commentcount.txt','w')python打开文件参数:
with open('c:\commentcount.txt','r') as fp:
counts = fp.readline()
counts = str(int(counts) + 1)
with open('c:\commentcount.txt','w') as fp:
fp.write(counts)
w
:
打开一个仅用于写入的文件。如果文件存在,则覆盖该文件。
如果文件不存在,则创建一个新文件以进行写入
您可以使用a
(附加):
打开要追加的文件。文件指针位于文件的末尾
如果文件存在。也就是说,文件处于追加模式。如果
文件不存在,它将创建一个新文件以供写入
有关更多信息,请阅读
还有一个建议是与以下内容一起使用:
with open("x.txt","a") as f:
data = f.read()
............
例如:
with open('c:\commentcount.txt','r') as fp:
counts = fp.readline()
counts = str(int(counts) + 1)
with open('c:\commentcount.txt','w') as fp:
fp.write(counts)
注意:只有当您有一个文件名commentcount
并且在第一行有一个int
时,这才有效,因为r
不会创建新文件,而且它只有一个计数器……它不会追加新的编号。有空文件
此问题是由于您未能关闭文件描述符造成的。您有f.close
,但它应该是f.close()
(函数调用)。最后还需要一个f2.close()
如果没有关闭
,缓冲区的内容将需要一段时间才能到达文件中。而且,一不使用文件描述符,就立即关闭它们是一种很好的做法
作为补充说明,您可以使用以下语法来确保尽快关闭文件描述符:
with open(file, mode) as f:
do_something_with(f)
现在,关于覆盖部分:
写入文件而不覆盖以前的内容。
简短回答:您没有以正确的模式打开文件。使用追加模式(“a”
)
长答案:
这是预期的行为。阅读以下内容:
>>> help(open)
Help on built-in function open in module __builtin__:
open(...)
open(name[, mode[, buffering]]) -> file object
Open a file using the file() type, returns a file object. This is the
preferred way to open a file. See file.__doc__ for further information.
>>> print file.__doc__
file(name[, mode[, buffering]]) -> file object
Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
writing or appending. The file will be created if it doesn't exist
when opened for writing or appending; it will be truncated when
opened for writing. Add a 'b' to the mode for binary files.
Add a '+' to the mode to allow simultaneous reading and writing.
If the buffering argument is given, 0 means unbuffered, 1 means line
buffered, and larger numbers specify the buffer size. The preferred way
to open a file is with the builtin open() function.
Add a 'U' to mode to open the file for input with universal newline
support. Any line ending in the input file will be seen as a '\n'
in Python. Also, a file so opened gains the attribute 'newlines';
the value for this attribute is one of None (no newline read yet),
'\r', '\n', '\r\n' or a tuple containing all the newline types seen.
因此,阅读手册表明,如果您希望保留内容,应在追加模式下打开:
open(file, "a")
您应该使用with语句。这假设无论发生什么情况,文件描述符都已关闭:
with open('file', 'r') as fd:
value = int(fd.read())
with open('file', 'w') as fd:
fd.write(value + 1)
你永远不会关闭文件。如果没有正确关闭文件,操作系统可能不会提交任何更改。为了避免这个问题,建议您使用Python的with
语句来打开文件,因为一旦处理完文件,它将为您关闭这些文件
with open('my_file.txt', a) as f:
do_stuff()
可以多次打开同一文件。你会在里面有不同的光标。