Advertisement
open('filename', mode);
File Mode | Description |
---|---|
a | Searches for the file and opens it in append mode i.e. this mode allows you to append new data to the end of a file. If file is not found, a new file is created. Allows you to append to the file by setting the cursor position to the end of file. |
Methods | Description |
---|---|
write(str) | The write() method writes and appends a string(str) value to the end of file. |
writelines(lines) | The writelines() method writes and appends a list of lines to the end of a file. |
Advertisement
Greetings! Hope you are doing well.
# Python - Appending new data to a file using the write() method.
# Calling the open() function to open a file named File1.txt in write mode
file = open('File1.txt', 'a')
# Creating 3 strings and appending the newline character \n at their beginning of each string
# to make the appended string appear in the new line.
str1 = '\n' + 'Never give up and keep learning.'
str2 = '\n' + 'Keep smiling :-)'
# Writing the first string value to the file using the write() method of file object
file.write(str1)
# Writing the second value to the file using the write() method of file object
file.write(str2)
# Closing the file after completing the write operation on it
file.close()
Greetings! Hope you are doing well.
Never give up and keep learning
Keep Smiling :-)
Fun facts!
# Python - Appending new data to the file by taking input from the user.
# Calling the open() function to open a file named File2.txt in write mode
file = open('File2.txt', 'a')
# Creating 3 strings
str1 = input('Which month has 28 days? : ')
str2 = input('What is the color of a block box on an airplane : ')
str3 = input('What comes down but never goes up? : ')
# Writing the first string value to the file using the write() method of file object
# Also, appending the newline character \n at their beginning of each string
# to make the appended string appear in the new line.
file.write('\n' + str1)
# Writing the second value to the file using the write() method of file object
file.write('\n' + str2)
# Writing the third string value to the file using the write() method of file object
file.write('\n' + str3)
# Closing the file after completing the write operation on it
file.close()
Which month has 28 days? : All months have 28 days :-)
What is the color of a block box on an airplane : The color of black box is always bright, so it is easy to find it.
What comes down but never goes up? : Rain always comes down, does it ever go up? Nah :-)
Fun Facts!
All months have 28 days :-)
The color of black box is always bright, so it is easy to find it.
Rain always comes down, does it ever go up? Nah :-)
All work and no plan could make Jack a dull boy!
# Python - Appending lines to a file.
# Calling the open() function to open a file named File3.txt in write mode
file = open('File7.txt', 'a')
# Creating 3 strings and appending the newline character \n at their beginning
# To make the appended string appear in the new line.
str1 = '\n' + 'Hello World!'
str2 = '\n' + 'Can you answer a question?'
str3 = '\n' + 'Which came first - Chicken or Egg?'
# Creating a list of lines, where each line is a string value.
a_list = [str1, str2, str3]
# Writing a list of lines to the file using the writelines() method of file object
file.writelines(a_list)
# Closing the file after completing the write operation on it
file.close()
All work and no plan could make Jack a dull boy!
Hello World!
Can you answer a question?
Which came first - Chicken or Egg?
Hello there!
# Python - Modifying a file
import os
# Calling the open() function to open a file named File3.txt in append + read mode
file = open('File11.txt', 'a+')
# 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
# Setting the cursor position to zero bytes from the beginning of the file
# So we could read the file from the beginning.
file.seek(0, os.SEEK_SET)
# Calling the read() method for file objects, to read all its content.
print('File content before appending : ')
print(file.read())
# Creating a string and appending the newline character \n at their end
str1 = 'Ting Tong!'
# 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 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 after appending data to it
print('File content before appending : ')
print(file.read())
# Closing the file after completing the write operation on it
file.close()
File content before appending :
Hello World. Keep Smiling!
File content after appending :
Hello World. Keep Smiling!Ting Tong!
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement