Advertisement
open('filename', mode);
File Mode | Description |
---|---|
r | Searches for the file and opens it in read mode only(if the file is found). This is also a default value. |
Methods | Description |
---|---|
read(n) | The read(n) method reads all the characters from the file in a string(until the EOF(end of file) is reached) and returns the string value. The n parameter is optional, but when specified, the read method reads only the n number of bytes from the file. |
readline(n) | This method reads a single line from a file and returns it. The n parameter is optional, but when specified, the read method reads only the n number of bytes from the file. |
readlines(n) | This method reads a list of lines from the file and returns the list. The n parameter is optional, but when specified, the read method reads only the n number of bytes from the file. |
Advertisement
Hello there!
How are you doing?
May you have a blessed day!
# Python - Reading the content of a file using the built-in open() function and the read() method
# Number of bytes to read
bytes_to_read = 60
# Calling the open() function to open a file named File.txt in read mode
file = open('File.txt', 'r')
# Calling the read() method for file objects, to read all its content.
print(file.read())
# Calling the close() method for file objects, to close the file after we have read it.
file.close()
Hello there!
How are you doing?
May you have a blessed day!
Good day!
Keep smiling
Dont'give up!
# Python - Reading the content of a file using the built-in open() function.
# Number of bytes to read
bytes_to_read = 5
# Calling the open() function to open a file named File2.txt in read mode
file = open('File2.txt', 'r')
# Calling the read() method for file objects, to read first 5 bytes
print('read(5) : ', file.read(bytes_to_read))
# Calling the read() method for file objects, to read its all leftover unread content.
print('read() : ', file.read())
# Calling the close() method for file objects, to close the file after we have read it.
file.close()
read(5) : Good
read() : day!
Keep smiling
Dont'give up!
Hello there!
How are you doing?
May you have a blessed day!
# Python - Reading the content of a file using the built-in open() function and the read() method
# Number of bytes to read
bytes_to_read = 60
# Calling the open() function to open a file named File.txt in read mode
file = open('File.txt', 'r')
# Calling the readline() method for file objects, to read a single line
print('readline() : ', file.readline())
# Calling the readline() method for file objects, to read the next 4 bytes in a file
print(readline(4) : ', file.readline(4))
# Calling the readline() method for file objects, to read the next single line
print(readline() : ', file.readline())
# Calling the close() method for file objects, to close the file after we have read it.
file.close()
readline() : Hello there!
readline(4) : How
readline() : are you doing?
Hello there!
How are you doing?
May you have a blessed day!
# Python - Reading the content of a file using the built-in open() function and the read() method
# Number of bytes to read
bytes_to_read = 60
# Calling the open() function to open a file named File.txt in read mode
file = open('File1.txt', 'r')
# Calling the readline() method for file objects, to read a single line
print('readlines() : ', file.readlines())
# Calling the readline() method for file objects, to read the next 4 bytes in a file
print('readlines(4) : ', file.readlines(4))
# Calling the readline() method for file objects, to read the next single line
print('readlines() : ', file.readlines())
# Calling the close() method for file objects, to close the file after we have read it.
file.close()
readlines() : ['Hello there!\n', 'How are you doing? \n', 'May you have a blessed day!']
readlines(4) : []
readlines() : []
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement