Python Read Contents of File and Plot
Table of Contents Hide
- Steps to Read Text File in Python
- Python open() part
- Methods for Reading file contents
- Python close() function
- Examples for Reading a Text file in Python
- Instance 1 – Read the entire text file using the read() function
- Example 2 – Read the specific length of characters in a text file using the read() role
- Example 3 – Read a single line in a file using the readline() part
- Example 4- Read text file line by line using the readline() function
- Case 5 – Read all the lines as a list in a file using the readlines() function
Python provides born functions to perform file operations, such as creating, reading, and writing files. At that place are mainly two types of files that Python tin can handle, normal text files and binary files. In this tutorial, we will have a look at how to read text files in Python.
Steps to Read Text File in Python
In Python, to read a text file, you need to follow the below steps.
Step 1: The file needs to be opened for reading using the open()
method and pass a file path to the function.
Step 2: The next step is to read the file, and this can be accomplished using several built-in methods such equally read()
, readline()
, readlines()
.
Step iii: In one case the read operation is performed, the text file must be closed using the close()
part.
At present that we take seen the steps to read the file content allow'southward understand each of these methods before getting into examples.
Python open()
function
The open up()
function opens the file if possible and returns the corresponding file object.
Syntax – open(file, mode='r', buffering=-ane, encoding=None, errors=None, newline=None, closefd=True, opener=None)
The open()
office has a lot of parameters. Let's take a wait at the necessary params for reading the text file. It opens the file in a specified manner and returns a file object.
Parameters
- file – path like object which represents the file path
- mode (Optional) – The
fashion
is an optional parameter. It's a string that specifies the manner in which you lot want to open up the file.
Mode | Clarification |
---|---|
'r' | Open a file for read way (default if mode is not specified) |
'due west' | Open up a file for writing. Python volition create a new file if does non be or truncates a file content if file exists |
'x' | Open a file for exclusive creation. |
'a' | Open a file for appending the text. Creates a new file if file does not exist. |
't' | Open up a file in text way. (default) |
'b' | Open a file in binary mode. |
'+' | Open a file for updating (reading and writing) |
Case
file = open('C:\hello.txt','r')
Methods for Reading file contents
There are iii ways to read data from a text file.
-
read()
: Theread()
function returns the read bytes in the grade of string. This method is useful when you have a small file, and you lot desire to read the specified bytes or unabridged file and store information technology into a string variable. -
readline()
: Thereadline()
function returns ane line from a text file and retuns in the class of string. -
readlines()
: Thereadlines()
office reads all the lines from the text file and returns each line as a string element in a list.
Python shut()
role
The file will remain open until y'all close the file using the shut()
function. Information technology is a must and best practice to perform this functioning after reading the information from the file every bit information technology frees up the memory space caused by that file. Otherwise, it may cause an unhandled exception.
Examples for Reading a Text file in Python
Example ane – Read the entire text file using the read()
function
In the below example, we are reading the entire text file using the read()
method. The file can be opened in the read mode or in a text mode to read the data, and information technology tin be stored in the string variable.
# Program to read the entire file using read() function file = open("python.txt", "r") content = file.read() print(content) file.shut() # Program to read the unabridged file (absolute path) using read() function file = open("C:/Projects/Tryouts/python.txt", "r") content = file.read() print(content) file.close()
Output
Dear User, Welcome to Python Tutorial Accept a keen learning !!! Cheers
Example 2 – Read the specific length of characters in a text file using the read()
function
There are times where yous need to read the specific bytes in a file. In that example, y'all can use the read()
role by specifying the bytes. The method will output only the specified bytes of characters in a file, as shown below.
# Program to read the specific length # of characters in a file using read() role file = open("python.txt", "r") content = file.read(20) print(content) file.shut()
Output
Dear User, Welcome
Example iii – Read a single line in a file using the readline()
office
If you want to read a single line in a file, then you lot could achieve this using readline()
part. You too use this method to think specific bytes of characters in a line, similar to the read()
method.
# Program to read unmarried line in a file using readline() office file = open("python.txt", "r") content = file.readline() print(content) file.close()
Output
Honey User,
Instance 4- Read text file line by line using the readline()
function
If yous want to traverse the file line by line and output in any format, so you could apply the while loop with the readline()
method as shown below. This is the nearly effective way to read the text file line by line in Python.
# Program to read all the lines in a file using readline() function file = open("python.txt", "r") while True: content=file.readline() if non content: pause print(content) file.close()
Output
Beloved User, Welcome to Python Tutorial Have a dandy learning !!! Thanks
Example 5 – Read all the lines as a list in a file using the readlines()
function
The readlines()
method will read all the lines in the file and outputs in a list of strings, as shown below. Later you lot tin use the list to traverse and extract the specified content from the listing.
# Program to read all the lines as a list in a file # using readlines() part file = open("python.txt", "r") content=file.readlines() print(content) file.shut()
Output
['Dear User,\n', 'Welcome to Python Tutorial\northward', 'Have a not bad learning !!!\north', 'Cheers']
Sign Up for Our Newsletters
Source: https://itsmycode.com/python-read-text-file/
0 Response to "Python Read Contents of File and Plot"
Post a Comment