医生说,

模式'r+', 'w+'和'a+'打开文件进行更新(注意'w+'会截断文件)。在区分二进制文件和文本文件的系统上,在模式后追加'b'以二进制模式打开文件;在没有这种区别的系统中,添加“b”没有任何效果。

这里

w+:打开文件进行读写操作。如果文件存在,则覆盖现有文件。如果该文件不存在,则创建一个新文件进行读写。

但是,如何用w+读取打开的文件?


当前回答

我认为有两种方法可以解决你想要达到的目标。

1)这是显而易见的,打开文件只读,读入内存,然后用t打开文件,然后写你的更改。

2)使用低级文件处理例程:

# Open file in RW , create if it doesn't exist. *Don't* pass O_TRUNC
 fd = os.open(filename, os.O_RDWR | os.O_CREAT)

希望这能有所帮助。

其他回答

下面是打开文件的不同模式:

r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. r+ Opens a file for both reading and writing. The file pointer will be at the beginning of the file. rb+ Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file. w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

我认为有两种方法可以解决你想要达到的目标。

1)这是显而易见的,打开文件只读,读入内存,然后用t打开文件,然后写你的更改。

2)使用低级文件处理例程:

# Open file in RW , create if it doesn't exist. *Don't* pass O_TRUNC
 fd = os.open(filename, os.O_RDWR | os.O_CREAT)

希望这能有所帮助。

两者似乎都是一样的,但有一个问题。

r +待遇:

打开文件进行读写 一旦在开始文件中打开,指针将指向0 现在如果你想要阅读,它会从头开始阅读 如果你想写,那么就开始写,但是写过程将从指针0开始。如果有的话,字符会被覆盖 在这种情况下,File应该存在,FileNotFoundError将被引发。

w +待遇:

打开文件进行读写 如果文件存在,文件将被打开,所有数据将被擦除, 如果文件不存在,则创建新文件 在开始文件中指针将指向0(因为没有数据) 现在如果你想写点什么,那就写吧 文件指针现在指向文件的末尾(写入过程之后) 如果你现在想读取数据,请寻找特定的点。(用于开始seek(0))

总的来说,两者都意味着打开文件进行读写,但区别在于我们是否想要在开始时删除数据,然后进行读/写,还是只是像现在这样开始。

txt -在开始

1234567
abcdefg
0987654
1234

r+的代码

with open('abc.txt', 'r+') as f:      # abc.txt should exist before opening
    print(f.tell())                   # Should give ==> 0
    f.write('abcd')                   
    print(f.read())                   # Pointer is pointing to index 3 => 4th position
    f.write('Sunny')                  # After read pointer is at End of file

输出

0
567
abcdefg
0987654
1234

abc.txt -运行后:

abcd567
abcdefg
0987654
1234Sunny

将abc.txt重置为初始值

w+的代码

with open('abc.txt', 'w+') as f:     
    print(f.tell())                   # Should give ==> 0
    f.write('abcd')                   
    print(f.read())                   # Pointer is pointing to index 3 => 4th position
    f.write('Sunny')                  # After read pointer is at End of file

输出

0


abc.txt -运行后:

abcdSunny

Python中的所有文件模式

r for reading r+ opens for reading and writing (cannot truncate a file) w for writing w+ for writing and reading (can truncate a file) rb for reading a binary file. The file pointer is placed at the beginning of the file. rb+ reading or writing a binary file wb+ writing a binary file a+ opens for appending ab+ Opens a file for both appending and reading in binary. The file pointer is at the end of the file if the file exists. The file opens in the append mode. x open for exclusive creation, failing if the file already exists (Python 3)

假设你用一个with语句打开文件,就像你应该做的那样。然后你可以这样从你的文件中读取:

with open('somefile.txt', 'w+') as f:
    # Note that f has now been truncated to 0 bytes, so you'll only
    # be able to read data that you write after this point
    f.write('somedata\n')
    f.seek(0)  # Important: return to the top of the file before reading, otherwise you'll just read an empty string
    data = f.read() # Returns 'somedata\n'

注意f.seek(0)——如果您忘记了这一点,f.read()调用将尝试从文件末尾读取,并返回一个空字符串。