Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookReading & Writing Text Files

Swipe to show menu

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.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 1
some-alt