Advertisement
open('filename', mode);
File Mode | Description | r+ | Searches for the file and opens it in read plus update mode.(if the file is found). |
---|---|
w+ | Searches for the file and opens it in write mode. If the file is found, its content is overwritten. If file is not found, a new file is created. Allows you to first write to the file and then read it and reading a file before writing to it, will return an empty string. |
Advertisement
This is a beautiful day!
# Python - Modifying a file
import os
# Calling the open() function to open a file named File3.txt in write mode
file = open('File1.txt', 'w+')
# Creating a string and appending the newline character \n at their end
str1 = 'Hello World. Keep Smiling!'
# Writing a string to the file using the write() method of file object
file.write(str1)
# Calling the seek method to set the cursor position to the zeroth bytes from the beginning of the file
# os.SEEK_SET = sets the cursor to the beginning of the file
# os.SEEK_CUR = sets the cursor to the current position in the file
# os.SEEK_END = sets the cursor to the end position in the file
file.seek(0, os.SEEK_SET)
# Calling the read() method for file objects, to read all its content.
print(file.read())
# Closing the file after completing the write operation on it
file.close()
Hello World. Keep Smiling!
Live and let live!
# Python - Appending lines to a file.
import os
# Calling the open() function to open a file named File3.txt in write mode
file = open('File2.txt', 'r+')
# Calling the read() method for file objects, to read all its content.
print('Reading the old content of the file :')
print(file.read())
# Creating a string and to write to the file
str1 = 'Freedom!'
# Calling the seek method to set the cursor position to 4 bytes forward from the beginning position of the file.
# os.SEEK_SET = sets the cursor to the beginning of the file
# os.SEEK_CUR = sets the cursor to the current position in the file
# os.SEEK_END = sets the cursor to the end position in the file
file.seek(4, os.SEEK_SET)
# Writing a string to a file file using the write() method of file object
file.write(str1)
# Calling the seek method to set the cursor position to zero bytes from the current position.
# Because we are going to read the file again, to read its updated content
file.seek(0, os.SEEK_SET)
# Calling the read() method for file objects, to read its updated content.
print('Reading the old content of the file :')
print(file.read())
# Closing the file after completing the write operation on it
file.close()
Reading the old content of the file :
Live and let live!
Reading the old content of the file :
LiveFreedom! live!
LiveFreedom! live!
GendXr : Female
Age : 23
City : Istanbul
Weight : 56.6 Kg
# Python - Modifying a file using fileinput and sys module
import sys
import fileinput
# This for loop scans and searches each line in the file
# By using the input() method of fileinput module
for line in fileinput.input("File3.txt", inplace=True):
# This will replace string "a" with "truck" in each line
line = line.replace("e", "X")
# write() method of sys module redirects the .stdout is redirected to the file
sys.stdout.write(line)
Reading the old content of the file :
Live and let live!
Reading the old content of the file :
LiveFreedom! live!
GXndXr : FXmalX
AgX : 23
City : Istanbul
WXight : 56.6 Kg
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement