In this tutorial, we’ll learn how to properly read file in Python. We’ll going through multiple examples to understand how to practically read file in Python programming language.
Outline
- Introduction: Read File In Python
- Python File Read Method
- Example1: Read A Simple File In Python
- Example 2: Specify Length in Python File Read Method
- Example 3: Convert Read File In Python To List
- Example 4: Concatenate Data Of Two Files
- Conclusion
Introduction: Read File In Python
Files are used to store data. For demonstration, we’ll create a simple text file and get its data using Python file read method. We’ll also work with multiples files.
Let’s now jump right into its practical implementation.
Python File Read Method
We’ll be using the read method of file to read its content. The syntax is shown below:
file.read(length(optional))
- This read method takes an optional length argument.
- Its used to specify the length of characters to be fetched from the file.
- It the argument is not passed, then it’ll read the whole content of file.
Let’s now practically learn how to properly read file in Python.
Example1: Read A Simple File In Python
File
This is a Python Programming Language Its really good
You’ve to create a text file using .text extension (nameOfFile.text). In our case, we’ve given it a name of simpleFile.text.
Code
file=open('simpleFile.text') data=file.read() print(data) file.close()
Output
This is a Python Programming Language Its really good
- File is in the same folder so we’ve just specified its name with extension. If you want to fetch a file from some other folder then you need to specify the complete path like foldername/filename.text.
- We’ve used open function and passed the path of file as an argument to it. We also have stored the returned file in variable file, you can give it any name you want. By default, the open function reads the file. If you want to specify it explicitly then use this statement open(filePath,’r’). The character(r) is for reading.
- After that, we’ve used the read method of Python file to read its content and have stored it inside a variable.
- Finally, we’ve printed the data using Python print function.
- Its a good practice to close file after the work is done using its close method.
We can use the try finally block. Its a good practice. See below code:
try: file=open('simpleFile.text') data=file.read() print(data) finally: file.close()
We can use the with syntax to specify the same code in few lines. See below code:
with open('simpleFile.text') as file: data=file.read() print(data)
We haven’t specified any length of characters to be fetched from the file so by default, all the characters were fetched.
Example 2: Specify Length in Python File Read Method
file=open('simpleFile.text') data=file.read(8) print(data) data2=file.read(5) print(data2) file.close()
Output
This is a Pyt
- We’ve read file in Python using read method. But in this case, we’ve specified a length of characters as well.
- For demonstration, we’ve used the read method twice. We can see that first 8 characters were read and using the read method again results in reading the characters after 8(as they were already read).
Example 3: Convert Read File In Python To List
We can also convert the read data to a Python list using split method. See below code example:
file=open('simpleFile.text') data=file.read() listData=data.split() print(listData) file.close()
Output
['This', 'is', 'a', 'Python', 'Programming', 'Language', 'Its', 'really', 'good']
Click here if you want to learn Python string split method in detail.
Example 4: Concatenate Data Of Two Files
In this example, we’ll create 2 files and read their data. After that, we’ll concatenate both of them and store the data inside a new variable. See below code:
file1=open('simpleFile.text','r') file2=open('file2.text') data1=file1.read() data2=file2.read() joinedData= data1+'\n'+data2 print(joinedData) file1.close() file2.close()
Output
This is a Python Programming Language Its really good This is also a Python Programming Language file Its also really awesome
So this is how we can easily read file in Python. I’d love to see you share this article with your developer friends.
Don’t hesitate to ask if you still have any questions regarding how to read file in Python. I’ll be very glad to solve all your queries.
Conclusion
To conclude this tutorial, hope you now have a detailed practical knowledge of how to properly read file in Python. I’ll be very happy to receive your feedback on this post.
Do visit my other articles on Python programming. Links to some of them are listed below. Others can be found using the navigation bar or search box of this site. Thank you for reading this post.
You may like to read:
[3 Ways] How To Easily Format String In Python
[Solved] How To Convert Python Tuple To String
How To Easily Get Input In Python [Python Code Examples]
[Solved] How To Find Maximum Value In Python Tuple
How To Easily Reverse String In Python [Python Code Examples]
How To Easily Use Python String Split Method [Detailed Python Guide]
How To Change Flutter CircularProgressIndicator Stroke Width – Easy Flutter Example Code