Python Full Tutorial — Part 11

Real applications store data. File handling is how Python talks to files on your system.

1. Opening a File


file = open("data.txt", "r")
content = file.read()
file.close()
  

2. File Modes

3. Writing to a File


file = open("log.txt", "w")
file.write("Hello Python\n")
file.close()
  

4. Appending Data


file = open("log.txt", "a")
file.write("New entry\n")
file.close()
  

5. Using with (Best Practice)


with open("data.txt", "r") as file:
    print(file.read())
  

6. Reading Line by Line


with open("data.txt") as file:
    for line in file:
        print(line.strip())
  

7. Real Use Cases

Key Takeaway

File handling is the bridge between programs and persistence.

Next: Virtual Environments & pip

Disclaimer:
File operations can modify system data. Always handle carefully.