Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

File_Handling_Python

File handling in Python enables reading, writing, updating, and deleting files, allowing for permanent data storage. Various modes such as 'r', 'w', 'a', and 'x' dictate how files are accessed and modified. The document provides examples for creating, reading, writing, appending, and deleting files, along with best practices like using the 'with' statement for automatic file closure.

Uploaded by

Chandu Alagani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

File_Handling_Python

File handling in Python enables reading, writing, updating, and deleting files, allowing for permanent data storage. Various modes such as 'r', 'w', 'a', and 'x' dictate how files are accessed and modified. The document provides examples for creating, reading, writing, appending, and deleting files, along with best practices like using the 'with' statement for automatic file closure.

Uploaded by

Chandu Alagani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

File Handling in Python (For Beginners)

What is File Handling?


File handling in Python allows you to read, write, update, and delete files.
It helps store data permanently, even after the program stops running.

Modes in File Handling:


--------------------------------------
'r' -> Read mode (file must exist)
'w' -> Write mode (overwrites the file, creates if not exists)
'a' -> Append mode (adds content, creates if not exists)
'x' -> Create mode (fails if file exists)
'r+' -> Read and write (file must exist)
'w+' -> Write and read (overwrites file, creates if not exists)

Examples:

1. Creating a File
-----------------------
file = open("example.txt", "w")
file.write("Hello, this is a new file!")
file.close()

2. Reading a File
-----------------------
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()

3. Writing to a File (Overwrites Content)


-----------------------
file = open("example.txt", "w")
file.write("This will overwrite previous content.")
file.close()

4. Appending to a File
-----------------------
file = open("example.txt", "a")
file.write("\nAdding new content without deleting old data.")
file.close()

5. Reading Line by Line


-----------------------
file = open("example.txt", "r")
for line in file:
print(line)
file.close()

6. Using 'with' Statement (Auto Closes File)


-----------------------
with open("example.txt", "r") as file:
content = file.read()
print(content)

7. Checking If File Exists (Before Opening)


-----------------------
import os
if os.path.exists("example.txt"):
file = open("example.txt", "r")
print(file.read())
file.close()
else:
print("File does not exist.")
8. Deleting a File
-----------------------
import os
if os.path.exists("example.txt"):
os.remove("example.txt")
print("File deleted successfully.")
else:
print("File not found.")

9. Creating a File Only if It Doesn't Exist ('x' Mode)


-----------------------
try:
file = open("newfile.txt", "x")
file.write("This file is created.")
file.close()
except FileExistsError:
print("File already exists.")

10. Read and Write ('r+' Mode)


-----------------------
file = open("example.txt", "r+")
print("Before writing:", file.read())
file.seek(0)
file.write("Updated content!")
file.close()

11. Write and Read ('w+' Mode)


-----------------------
file = open("example.txt", "w+")
file.write("New data written!")
file.seek(0)
print("After writing:", file.read())
file.close()
Conclusion:
- Use 'r' to read, 'w' to write (overwrite), 'a' to append.
- 'r+' allows reading and writing (file must exist).
- 'w+' allows writing and reading (overwrites file).
- Always close files or use 'with open(...)'.
- Use 'os' module to check and delete files.

You might also like