Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Reading & Writing Text Files | File I/O & Data Handling
Introduction to Python with AI

bookReading & Writing Text Files

Working with text files is a common task in programming — for saving data, reading configs, or processing logs.

Text files store content as plain lines of text, separated by newline characters. In Python, you use the built-in "open" function to read and write .txt files.

Note
Example Prompts
  • Show how to read all lines from a notes.txt file using a "with" block.
  • Show how to write multiple lines of text to a file in write mode.
  • Show how to open a shopping_list.txt file from a shopping directory using a relative path.

Opening a File

To work with a file, first open it using Python's "open" function with two arguments: the file name and the mode.

Modes:

  • "r" → read;
  • "w" → write (overwrites existing content);
  • "a" → append to the end;
  • "x" → create new file (error if it already exists).

Reading a File

After opening a file, you can read its contents in different ways:

  • "read()" → entire file as one string;
  • "readline()" → a single line;
  • "readlines()" → a list of all lines.

Always close the file when finished, or use a "with" block to handle it automatically.

Writing to a File

To save data, open the file in write or append mode.

  • Write mode ("w") clears old content;
  • Append mode ("a") adds new text at the end.

Use "write()" to add text, and include "\n" if you need line breaks.

File Paths

If your file is not in the same folder as your script, you'll need to use a full or relative file path. Python handles both forward slashes / and double backslashes \\ depending on your operating system.

Summary

  • Use the "open" function with different modes to work with text files;
  • Use "read()", "readline()", or "readlines()" to read file content;
  • Use "write()" or "append()" to save new content;
  • Always close the file or use a "with" block;
  • Use file paths when working with files outside your project folder.

Try It Yourself

  1. Open a file called "notes.txt" in write mode;
  2. Write three lines of text into it;
  3. Reopen the file in read mode;
  4. Print its contents line by line.
Все було зрозуміло?

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

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

Секція 5. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

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

Suggested prompts:

Can you show me an example code for these steps?

What happens if "notes.txt" doesn't exist when I try to read it?

How do I handle errors when working with files in Python?

Awesome!

Completion rate improved to 5

bookReading & Writing Text Files

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

Working with text files is a common task in programming — for saving data, reading configs, or processing logs.

Text files store content as plain lines of text, separated by newline characters. In Python, you use the built-in "open" function to read and write .txt files.

Note
Example Prompts
  • Show how to read all lines from a notes.txt file using a "with" block.
  • Show how to write multiple lines of text to a file in write mode.
  • Show how to open a shopping_list.txt file from a shopping directory using a relative path.

Opening a File

To work with a file, first open it using Python's "open" function with two arguments: the file name and the mode.

Modes:

  • "r" → read;
  • "w" → write (overwrites existing content);
  • "a" → append to the end;
  • "x" → create new file (error if it already exists).

Reading a File

After opening a file, you can read its contents in different ways:

  • "read()" → entire file as one string;
  • "readline()" → a single line;
  • "readlines()" → a list of all lines.

Always close the file when finished, or use a "with" block to handle it automatically.

Writing to a File

To save data, open the file in write or append mode.

  • Write mode ("w") clears old content;
  • Append mode ("a") adds new text at the end.

Use "write()" to add text, and include "\n" if you need line breaks.

File Paths

If your file is not in the same folder as your script, you'll need to use a full or relative file path. Python handles both forward slashes / and double backslashes \\ depending on your operating system.

Summary

  • Use the "open" function with different modes to work with text files;
  • Use "read()", "readline()", or "readlines()" to read file content;
  • Use "write()" or "append()" to save new content;
  • Always close the file or use a "with" block;
  • Use file paths when working with files outside your project folder.

Try It Yourself

  1. Open a file called "notes.txt" in write mode;
  2. Write three lines of text into it;
  3. Reopen the file in read mode;
  4. Print its contents line by line.
Все було зрозуміло?

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

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

Секція 5. Розділ 1
some-alt