- Python File Open
- Example
- Example
- Read Only Parts of the File
- Example
- Read Lines
- Example
- Example
- Example
- Close Files
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- Python File Handling
- Open a File
- Specify File Mode
- Read a File
- Read Lines
- Write a File
- Write mode ‘w’
- Append mode ‘a’
- Read-write mode ‘r+’
- Write Multiple Lines
- Flush Output Buffer
- Close a File
- Create a New File
- Delete a File
- Check if File Exists
- Random Access
- How to make a file read only using Python
- Make a file read-only using Python
Python File Open
Assume we have the following file, located in the same folder as Python:
To open the file, use the built-in open() function.
The open() function returns a file object, which has a read() method for reading the content of the file:
Example
If the file is located in a different location, you will have to specify the file path, like this:
Example
Open a file on a different location:
Read Only Parts of the File
By default the read() method returns the whole text, but you can also specify how many characters you want to return:
Example
Return the 5 first characters of the file:
Read Lines
You can return one line by using the readline() method:
Example
Read one line of the file:
By calling readline() two times, you can read the two first lines:
Example
Read two lines of the file:
By looping through the lines of the file, you can read the whole file, line by line:
Example
Loop through the file line by line:
Close Files
It is a good practice to always close the file when you are done with it.
Example
Close the file when you are finish with it:
Note: You should always close your files, in some cases, due to buffering, changes made to a file may not show until you close the file.
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Python File Handling
Python provides a wide range of built-in functions for file handling. It makes it really easy to create, update, read, and delete files.
Open a File
When you want to work with a file, the first thing to do is to open it. You can open a file using open() built-in function specifying its name.
When you specify the filename only, it is assumed that the file is located in the same folder as Python. If it is somewhere else, you can specify the exact path where the file is located.
f = open(r'C:\Python33\Scripts\myfile.txt')
Remember! While specifying the exact path, characters prefaced by \ (like \n \r \t etc.) are interpreted as special characters. You can escape them using:
Specify File Mode
Here are five different modes you can use to open the file:
Character | Mode | Description |
‘r’ | Read (default) | Open a file for read only |
‘w’ | Write | Open a file for write only (overwrite) |
‘a’ | Append | Open a file for write only (append) |
‘r+’ | Read+Write | open a file for both reading and writing |
‘x’ | Create | Create a new file |
You can also specify how the file should be handled.
Character | Mode | Description |
‘t’ | Text (default) | Read and write strings from and to the file. |
‘b’ | Binary | Read and write bytes objects from and to the file.This mode is used for all files that don’t contain text (e.g. images). |
# Open a file for reading f = open('myfile.txt') # Open a file for writing f = open('myfile.txt', 'w') # Open a file for reading and writing f = open('myfile.txt', 'r+') # Open a binary file for reading f = open('myfile.txt', 'rb')
Because read mode ‘r’ and text mode ‘t’ are default modes, you do not need to specify them.
Read a File
Suppose you have the following file.
First line of the file. Second line of the file. Third line of the file.
To read its contents, you can use read() method.
# Read entire file f = open('myfile.txt') print(f.read()) # Prints: # First line of the file. # Second line of the file. # Third line of the file.
By default, the read() method reads the entire file. However, you can specify the maximum number of characters to read.
f = open('myfile.txt') print(f.read(3)) # Prints Fir print(f.read(5)) # Prints First
Read Lines
To read a single line from the file, use readline() method.
f = open('myfile.txt') print(f.readline()) # Prints First line of the file. # Call it again to read next line print(f.readline()) # Prints Second line of the file.
You can loop through an entire file line-by-line using a simple for loop.
f = open('myfile.txt') for line in f: print(line) # Prints: # First line of the file. # Second line of the file. # Third line of the file.
If you want to read all the lines in a file into a list of strings, use readlines() method.
# Read all the lines in a file into a list of strings f = open('myfile.txt') print(f.readlines()) # Prints: # ['First line of the file.\n', 'Second line of the file.\n', 'Third line of the file.']
Note that list(f) gives the same result as readlines()
Write a File
Use the write() built-in method to write to an existing file. Remember that you need to open the file in one of the writing modes (‘w’, ‘a’ or ‘r+’) first.
Write mode ‘w’
In ‘w’ mode the entire contents of the file are overwritten:
f = open('myfile.txt', 'w') f.write('Overwrite existing data.')
Append mode ‘a’
In ‘a’ mode, text is added to the end of the file:
f = open('myfile.txt', 'a') f.write(' Append this text.')
First line of the file. Second line of the file. Third line of the file. Append this text.
Read-write mode ‘r+’
In ‘r+’ mode, the file is partially overwritten:
f = open('myfile.txt', 'r+') f.write('---Overwrite content---')
---Overwrite content--- Second line of the file. Third line of the file.
Write Multiple Lines
To write multiple lines to a file at once, use writelines() method. This method accepts list of strings as an input.
f = open('myfile.txt', 'w') lines = ['New line 1\n', 'New line 2\n', 'New line 3'] f.writelines(lines)
New line 1 New line 2 New line 3
Flush Output Buffer
When you write to a file, the data is not immediately written to disk instead it is stored in buffer memory. It is written to disk only when you close the file or manually flush the output buffer.
# Flush output buffer to disk without closing f = open('myfile.txt', 'a') f.write('Append this text.') f.flush()
Close a File
It’s a good practice to close the file once you are finished with it. You don’t want an open file running around taking up resources!
Use the close() function to close an open file.
f = open('myfile.txt') f.close() # check closed status print(f.closed) # Prints True
There are two approaches to ensure that a file is closed properly, even in cases of error.
The first approach is to use the with keyword, which Python recommends, as it automatically takes care of closing the file once it leaves the with block (even in cases of error).
with open('myfile.txt') as f: print(f.read())
The second approach is to use the try-finally block:
f = open('myfile.txt') try: # File operations goes here finally: f.close()
Create a New File
If you try to open a file for writing that does not exist, Python will automatically create the file for you.
# Create a file and open it for writing # write mode f = open('newFile.txt', 'w') # append mode f = open('newFile.txt', 'a') # read + write mode f = open('newFile.txt', 'r+')
There is another way to create a file. You can use open() method and specify exclusive creation mode ‘x’.
# Create a file exclusively f = open('newFile.txt', 'x')
Note that when you use this mode, make sure that the file is not already present, if it is, Python will raise the error.
Delete a File
You can delete a file by importing the OS module and using its remove() method.
import os os.remove('myfile.txt')
Check if File Exists
If you try to open or delete a file that does not exist, an error occurs. To avoid this, you can precheck if the file already exists by using the isfile() method from the OS module.
import os if os.path.isfile('myfile.txt'): f = open('myfile.txt') else: print('The file does not exist.')
Random Access
Sometimes you need to read from the record in the middle of the file at that time you can use the seek(offset, from_what) method. This function moves the file pointer from its current position to a specific position.
The offset is the number of characters from the from_what parameter which has three possible values:
- 0 – indicates the beginning of the file (default)
- 1 – indicates the current pointer position
- 2 – indicates the end of the file
Suppose you have the following file.
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Here’s how you can use the seek() method to randomly access the contents of a file.
f = open('myfile.txt', 'rb+') # go to the 7th character and read one character f.seek(6) print(f.read(1)) # Prints G # go to the 3rd character from current position (letter G) f.seek(3, 1) print(f.read(1)) # Prints K # go to the 3rd character before the end f.seek(-3, 2) print(f.read(1)) # Prints X
For Python 3.2 and up, in text files (those opened without a b in the mode string), only seeks relative to the beginning of the file are allowed.
In order to use seek from current position and end, you have to open the text file in binary mode.
If you want to check the current position of the file pointer, use the tell() method.
f = open('myfile.txt') # initial position print(f.tell()) # Prints 0 # after reading 5 characters f.read(5) print(f.tell()) # Prints 5
How to make a file read only using Python
In this article, we will discuss how to modify the permissions of a file and make a file read-only using Python. You may need this for automating daily activities using Python scripts.
Make a file read-only using Python
Making the file read-only will not allow the file to be rewritten again. For this, we need to modify the permissions of the file. To achieve this, we will make use of the os module in Python more specifically, the chmod() of the os module.
The coding part is extremely simple and will contain very few lines as we are not doing much but changing the permissions. Using the chmod(), we can change the mode of the path, setting it to any mode using the suitable flags from the stat module. Both these modules come inbuilt with Python and hence you need not install anything additionally.
The entire code to change the file to read-only is as follows
import os from stat import S_IREAD # Replace the first parameter with your file name os.chmod("sample.txt", S_IREAD)
You can verify if the code was executed correctly by checking the file’s permissions. To do that :
- Right-click on the file and click properties.
- Under the attributes section, you will find the read-only checkbox checked.
I hope you found this article useful and it helped you make a file read-only. You can do more than just making the file read-only by using the appropriate flag from the stat module. You can find the appropriate flag for your use from the documentation.