Introduction – Python File Handling
- Python File Handling helps to store program output permanently.
- Normally, output is lost after closing IDLE.
- File handling lets us save output in a file for later use.
- Data files store data that can be accessed anytime.
- It’s similar to saving a document like a CV for future reference.
- Python allows creating, storing, and accessing files easily.
- The types of files with which we have to go through are:-
Text File
Binary File
CSV File
Relative and Absolute Path
- Relative path describe the location of a file relative to the working directory .
- Absolute path describe the location from the root directory.
How to work with File Handling?
- We have to follow the following points.
- Open a file
- First point to open a file , either a new file or a pre existing file, we may open the file for following purposes-
- Reading data from file – to read the content of the desired file.
- Writing data to file- to write the content into desired file
- Appending data to file- add some data in a pre-existing file
- Syntax for opening a file-
file = open("filename", "mode")
- filename → name/path of the file (e.g., “data.txt”)
- mode → defines how the file will be opened (read, write, append, etc.)
- Applying action on file
There are following file opening mode in a file handling in python:-
1. “r” – Read mode
-
Opens file for reading only
-
File must exist, otherwise error occurs
Syntax:
f = open("file.txt", "r")
2. “w” – Write mode
- Opens file for writing only
- Creates a new file if not exists
- Overwrites existing content
Syntax:
f = open("file.txt", "w")
3. “a” – Append mode
- Opens file for appending data
- Adds new data at the end of file
- Does not erase existing content
Syntax:
f = open("file.txt", "a")
4. “r+” – Read and Write mode
- Opens file for both reading and writing
- File must already exist
Syntax:
f = open("file.txt", "r+")
5. “w+” – Write and Read mode
- Opens file for reading and writing
- Creates new file if not exists
- Overwrites existing content
Syntax:
f = open("file.txt", "w+")
6. “a+” – Append and Read mode
- Opens file for appending and reading
- Creates file if not exists
- Cursor at end of file
Syntax:
f = open("file.txt", "a+")
- Close a file
A file can be closed by calling the close function using the file object.
Syntax:-
f.close()
7. seek ()- Move File Pointer
Used to change the current position of file pointer.
file.seek(position)
Example-
f = open("data.txt","r")
print(f.read(5))
f.seek(0)
print(f.read(5))
f.close()
Explanation:
seek(0)→ Moves pointer to beginning.seek(5)→ Moves pointer to 5th position.
8. tell()- Check Current Position
Returns the current position of file pointer.
f = open("data.txt","r")
print(f.tell())
f.read(4)
print(f.tell())
f.close()
Explanation:
After reading 4 characters, pointer position becomes 4.
9. flush()-Save Buffered Data
Forces data to be written immediately.
f = open("data.txt","w")
f.write("Hello")
f.flush()
f.close()
10- truncate ()-Resize File
Cuts the file to specified size.
f = open("data.txt","r+")
f.truncate(5)
f.close()
If file contains:
Programming
After truncate(5):
Progr