简体   繁体   English

我无法在 Python 3.9 代码中读取/写入文件

[英]I can't read/write a file in my Python 3.9 code

I don't know what happen, but my code doesn't want to work properly.我不知道会发生什么,但我的代码不想正常工作。 This is the code:这是代码:

with open("count.txt","w+") as f:
    print(f.read())
    a=int(f.read())
    b=int(a+1)
    print(str(b))
    f.write(str(b))
input()

I put the "count.txt" in same directory, and this is the content of "count.txt"我把“count.txt”放在同一个目录下,这就是“count.txt”的内容

0

and this is the error that I got:这是我得到的错误:

Traceback (most recent call last):
  File "C:\FILEDIRECTORY\plus.py", line 3, in <module>
    a=int(f.read())
ValueError: invalid literal for int() with base 10: ''

Then, the "count.txt" become blank (0 bytes).然后,“count.txt”变为空白(0 字节)。 I tried to change the mode to "r", but the same error happen, but the content of "count.txt" doesn't get erased.我试图将模式更改为“r”,但发生了同样的错误,但“count.txt”的内容没有被删除。 Then I try to change the mode to "w", change the f.write content to "1", and make the other code comment.然后我尝试将模式更改为“w”,将f.write内容更改为“1”,并对其他代码进行注释。

with open("count.txt","w") as f:
    '''print(f.read())
    a=int(f.read())
    b=int(a+1)
    print(str(b))'''
    f.write("1")
input()

But now it works.但现在它起作用了。 The "count:txt" content become "1"! “count:txt”的内容变成了“1”! I also try this :我也试试这个:

with open("count.txt","w") as f:
    '''print(f.read())
    a=int(f.read())
    b=int(a+1)
    print(str(b))'''
    a="1"
    f.write(a)
input()

And it still works, So i think the read mode is broken.它仍然有效,所以我认为读取模式已损坏。 but i don't know why?但我不知道为什么? Maybe I Install Python Incorrectly?也许我安装 Python 不正确?

f.read() reads the entire file and leaves the current position at the end of the file, so the 2nd f.read() call starts from the end of the file and doesn't read anything. f.read()读取整个文件并将当前 position 留在文件末尾,因此第二次f.read()调用从文件末尾开始并且不读取任何内容。 Instead, save the read content to a variable.相反,将读取的内容保存到变量中。

In addition, w+ truncates the file first, use r+ to avoid this.另外, w+会先截断文件,使用r+可以避免这种情况。

I'm also assuming that you want to clear the previous value in the file, so use f.truncate to clear the file and f.seek to reset the position to the start of the file.我还假设您要清除文件中的先前值,因此使用f.truncate清除文件并使用 f.seek 将f.seek重置为文件的开头。

Putting it all together:把它们放在一起:

with open("count.txt","r+") as f:
    content = f.read()
    print(content)
    a = int(content)
    b = a+1
    print(b)
    f.truncate(0)
    f.seek(0)
    f.write(str(b))
input()

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

相关问题 我无法在Python中读入文件并写出到文件 - I can't read in a file and write out to a file in Python 我无法读取我刚刚在 python 中写入的文件 - I can't read the file i just write in python 我正在使用 python 3.9,但我不知道如何在我的 Windows 10 中安装 pyaudio - I am using python 3.9 and I can't figure out how to install pyaudio in my Windows 10 当我打开要首先写入的文件时,为什么在File.open中无法读取任何内容? - Why I can't read anything with File.open in python when I open the file to write first? 如何在 Python 3.9(64 位)中运行此代码? - How can i run this code in Python 3.9 (64-bit)? 我无法在 vscode 中运行代码或调试我的 python 文件 - I can't run code or debug of my python file in vscode 我无法在 Python 3.9 中运行 while 循环 - I can't run a while loop in Python 3.9 无法读取文件,然后将结果写入scrapy(python) - Can't read in a file and then write results to a file with scrapy (python) 如何在 Databricks 上编写命令以读取 XML 文件(基于 Python 代码)? - How can I write the command on Databricks to read XML file (based on Python code)? 如何在 Windows 10 中使用 python3.9 检查文件是否在我的 python 脚本之外的另一个进程中主动打开? - How can I check if a file is actively open in another process outside of my python script with python3.9 in Windows 10?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM