Possible Duplicate:
python open built-in function: difference between modes a, a+, w, w+, and r+?
try:
f = open("file.txt", "r")
try:
string = f.read()
line = f.readline()
lines = f.readlines()
finally:
f.close()
except IOError:
pass
try:
f = open("file.txt", "w")
try:
f.write('blah') # Write a string to a file
f.writelines(lines) # Write a sequence of strings to a file
finally:
f.close()
except IOError:
pass
Hi,
this is mode which i can read and write file but i want to open file once and perform both read and write operation in python
r+ opens for reading and writing (no truncating, file pointer at the beginning) w+ opens for writing (and thus truncates the file) and reading. a+ opens for appending (writing without truncating, only at the end of the file, and the file pointer is at the end of the file) and reading.
The shutil. copy() method in Python is used to copy the content of the source file to destination file or directory.
'r+' opens the file for both reading and writing.
Like in any other programming languages you can open a file in r+
, w+
and a+
modes.
r+
opens for reading and writing (no truncating, file pointer at the beginning)w+
opens for writing (and thus truncates the file) and readinga+
opens for appending (writing without truncating, only at the end of the file, and the file pointer is at the end of the file) and readingFrom the doc:
r+
: opens the file for both reading and writing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With