Phonex_pro

1. Opening a File

The current image has no alternative text. The file name is: ba.jpg

Python uses the built-in open() function:

# Syntax: open(filename, mode)
file = open("example.txt", "r")

File Modes

ModeDescription
"r"Read (default) – opens file for reading; error if file doesn’t exist
"w"Write – creates file or overwrites existing file
"a"Append – creates file if not exists, adds content at the end
"x"Create – creates file; error if file exists
"b"Binary mode (e.g., "rb", "wb")
"t"Text mode (default, e.g., "rt")

2. Reading from a File

file = open("example.txt", "r")
content = file.read()   # Reads entire file
print(content)
file.close()

Other methods:

file.readline()   # Reads one line
file.readlines()  # Reads all lines as a list

3. Writing to a File

file = open("example.txt", "w")
file.write("Hello, Python!\n")
file.write("This is file handling.\n")
file.close()
  • If "w" is used, the old content is erased.
  • Use "a" mode to append instead:
file = open("example.txt", "a")
file.write("Adding new line without erasing old content.\n")
file.close()

4. Using with Statement

It is recommended to use with to avoid forgetting close():

with open("example.txt", "r") as file:
    for line in file:
        print(line.strip())
  • Automatically closes the file after block execution.

5. Handling Errors in File Operations

Often combined with exception handling:

try:
    with open("data.txt", "r") as file:
        print(file.read())
except FileNotFoundError:
    print("File not found!")

Summary

  • Use open(filename, mode) to work with files.
  • "r", "w", "a", "x", "b", and "t" are common file modes.
  • Always close files after use, or better use with.
  • Combine file handling with exception handling for safer code.

⬇️ Download Phonex_pro تحميل Server 1

Related Posts

Dauo

1. Introduction Web scraping is the process of extracting data from websites. In Python, we commonly use libraries like requests (to fetch web pages) and BeautifulSoup (to parse and extract…

Read more

Macia_Pro

1. What is an API? 2. The requests Library The most common library for working with APIs in Python. Install if needed: 3. Making a GET Request ✅ GET requests…

Read more

Drag_Club

1. What is JSON? Example JSON data: 2. Working with JSON in Python Python has a built-in json module. 3. Converting JSON to Python (Parsing) 4. Converting Python to JSON…

Read more

BEASh

1. What Is a Module? A module is simply a Python file (.py) that contains functions, classes, or variables you can reuse in other programs. Example: mymodule.py Using it in…

Read more

Tigriptv

Introduction Errors are a normal part of programming. In Python, you will encounter two main types of errors: Python provides a powerful way to handle exceptions so your program doesn’t…

Read more

Cleo_patra

1. Introduction File handling is one of the most important skills in Python. Almost every real-world application needs to read from or write to a file. Python makes file handling…

Read more