Here is a clear, complete, and exam-oriented explanation of Operations on Files in Python — covering opening, modes, attributes, encoding, and closing.
⭐ Operations on Files in Python
Python provides powerful features for working with files. The main operations include:
✔ Opening
✔ Reading
✔ Writing
✔ Checking attributes
✔ Encoding
✔ Closing
Let’s discuss each of these in detail.
🔶 1. Opening a File
Files are opened using the built-in open() function.
✅ Syntax
file_object = open(filename, mode, encoding)
Example:
f = open("data.txt", "r")
If no mode is specified, default mode = "r" (read mode).
🔶 2. File Modes
File modes define what operations can be performed on the file.
| Mode | Meaning | Description |
|---|---|---|
"r" | Read | Opens file for reading (default). Error if file doesn’t exist. |
"w" | Write | Opens file for writing. Overwrites file or creates a new one. |
"a" | Append | Opens file for appending. Adds content to end without deleting old data. |
"r+" | Read + Write | File must already exist. |
"w+" | Write + Read | Overwrites existing file or creates new one. |
"a+" | Append + Read | File pointer at end. Reads allowed. |
"b" | Binary mode | Used for binary files (images, videos). |
"t" | Text mode | Default mode (for .txt files). |
Examples:
open("notes.txt", "w") # write mode
open("photo.jpg", "rb") # binary read
open("log.txt", "a") # append mode
🔶 3. File Attributes
Once a file is opened, you can check its attributes:
f = open("data.txt", "r")
print(f.name) # file name
print(f.mode) # file mode
print(f.closed) # True/False
print(f.encoding) # file encoding
📌 Common File Attributes
| Attribute | Description |
|---|---|
file.name | Name of the file |
file.mode | Mode used to open |
file.closed | True if file is closed |
file.encoding | Encoding type |
file.readable() | True if reading allowed |
file.writable() | True if writing allowed |
🔶 4. Encoding
Encoding defines how characters are stored in a file.
Common encodings:
"utf-8"→ standard encoding"ascii"→ limited to English characters
Example:
f = open("data.txt", "w", encoding="utf-8")
f.write("Hello, स्वागत है!")
f.close()
⚠ If encoding is not specified, Python uses system default (usually UTF-8).
🔶 5. Closing a File
After completing operations, always close the file to free memory and avoid data corruption.
Example:
f = open("data.txt", "r")
content = f.read()
f.close()
🔶 Best Practice: Using with Statement
with automatically closes the file, even if an error occurs.
with open("data.txt", "r") as f:
print(f.read())
print(f.closed) # True
✔ No need to call close()
✔ Safer and cleaner code
✔ Prevents memory leaks
⭐ Complete Example: Using All Operations
# Opening file with UTF-8 encoding
f = open("sample.txt", "w", encoding="utf-8")
# Writing data
f.write("Hello Students!\nWelcome to File Handling.")
print("File Name:", f.name)
print("Mode:", f.mode)
print("Encoding:", f.encoding)
# Closing file
f.close()
⭐ Exam-Ready Short Notes
✔ Opening
Use open(filename, mode, encoding) to open files.
✔ Modes
"r"read"w"write"a"append"b"binary"t"text"+“reading + writing
✔ File Attributes
Use attributes like .name, .mode, .closed, .encoding.
✔ Encoding
Specifies how characters are stored. "utf-8" is most common.
✔ Closing
Use .close() or with statement for automatic closing.
⭐ Perfect 5–6 Line Examination Answer
In Python, file operations include opening a file using the open() function, performing read/write operations using different modes such as "r", "w", "a", "b", and "t". File attributes like name, mode, closed, and encoding give details about the file. Encoding specifies how text is stored (e.g., UTF-8). Finally, files must be properly closed using close() or the with statement to free resources and avoid data loss.
