Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Reading and Writing Files: Handling Text and Data Efficiently | Mastering File Handling in Python
Advanced Python

bookReading and Writing Files: Handling Text and Data Efficiently

Reading from a File

To start reading a file, you first need to open it in the appropriate mode. Here's how you can open and read from a file:

file = open("greetings.txt", "r")
print(file.read()) # Output:
# Hello, world!
# Salut!
# Hola!
file.close()

Reading Specific Characters

You can also read a specific number of characters by passing a numeric argument to the read method:

file = open("greetings.txt", "r")
print(file.read(10))  # Output: Hello, wor
file.close()

Reading Line by Line

To read a file line by line, you can use a loop along with the readline() or readlines() methods. The readline() method returns a string for each line, while readlines() returns a list of all lines:

file = open("greetings.txt", "r") # ['Hello, world!\n', 'Salut!\n']
print(file.readline())  # Output: Hello, world!
print(file.readline())  # Output: Salut!
file.close()

For more efficient line-by-line reading without loading the entire file into memory, use a for loop with readlines():

file = open("greetings.txt", "r")
for line in file.readlines():
    print(line, end="")  # The `end=""` argument prevents adding extra line breaks
# Output:
# Hello, world!
# Salut!
# Hola!
file.close()

Writing to a File

To write data to a file, you should open it in write mode ('w'). Only strings can be passed to the write() method.

file = open("greetings.txt", "w")
file.write("Bonjour!")
file.close()

Note

The writing mode overwrites the existing file content.

In this case, any existing data in "greetings.txt" will be replaced with "Bonjour!"

question mark

Choose the right statement for the 'r' mode:

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 3.13

bookReading and Writing Files: Handling Text and Data Efficiently

Свайпніть щоб показати меню

Reading from a File

To start reading a file, you first need to open it in the appropriate mode. Here's how you can open and read from a file:

file = open("greetings.txt", "r")
print(file.read()) # Output:
# Hello, world!
# Salut!
# Hola!
file.close()

Reading Specific Characters

You can also read a specific number of characters by passing a numeric argument to the read method:

file = open("greetings.txt", "r")
print(file.read(10))  # Output: Hello, wor
file.close()

Reading Line by Line

To read a file line by line, you can use a loop along with the readline() or readlines() methods. The readline() method returns a string for each line, while readlines() returns a list of all lines:

file = open("greetings.txt", "r") # ['Hello, world!\n', 'Salut!\n']
print(file.readline())  # Output: Hello, world!
print(file.readline())  # Output: Salut!
file.close()

For more efficient line-by-line reading without loading the entire file into memory, use a for loop with readlines():

file = open("greetings.txt", "r")
for line in file.readlines():
    print(line, end="")  # The `end=""` argument prevents adding extra line breaks
# Output:
# Hello, world!
# Salut!
# Hola!
file.close()

Writing to a File

To write data to a file, you should open it in write mode ('w'). Only strings can be passed to the write() method.

file = open("greetings.txt", "w")
file.write("Bonjour!")
file.close()

Note

The writing mode overwrites the existing file content.

In this case, any existing data in "greetings.txt" will be replaced with "Bonjour!"

question mark

Choose the right statement for the 'r' mode:

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2
some-alt