# Nakafa Framework: LLM URL: /en/subject/university/bachelor/ai-ds/ai-programming/file-input-output Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/file-input-output/en.mdx Output docs content for large language models. --- export const metadata = { title: "File Input and Output", description: "Learn how to read and write files in Python, file opening modes, read(), readline() methods, and output redirection with practical examples.", authors: [{ name: "Nabil Akbarazzima Fatih" }], date: "09/18/2025", subject: "AI Programming", }; ## Reading Files In Python, reading files is the process of retrieving data from files stored in the system. Think of it like opening a book and reading its contents, Python can open files and access the information within them. ### Reading Entire File Content The simplest way to read a file is to take all its contents at once into memory. This process involves three main steps. First, open the file with the `open()` function. Second, read its contents with the `read()` method. Third, close the file with the `close()` method to free system resources. ### Reading File Line by Line For large files, reading line by line is more efficient because it doesn't load the entire file into memory at once. When reading line by line, each line retains the newline character (`\n`) at the end. The `end=''` parameter in the `print()` function prevents adding an additional newline. ## Writing to Files Writing files allows programs to save data or calculation results into files for later use. ### Writing Strings to Files ### Redirecting Print Output to File You can redirect the output of the `print()` function directly to a file using the `file` parameter. This method is very useful when you want to save program output results to a file without changing the existing code structure. ## File Opening Modes Python provides various modes for opening files according to the needs of the operations to be performed. B[Text Mode] A --> C[Binary Mode] B --> B1[r - read only] B --> B2[w - write new] B --> B3[a - append end] B --> B4[r+ - read and write] C --> C1[rb - read binary] C --> C2[wb - write binary] C --> C3[ab - append binary] C --> C4[rb+ - read write binary] `} /> ### File Opening Syntax The `mode` parameter determines how the file will be accessed. Mode `r` for reading, `w` for writing (overwrites old file), `a` for appending to the end of file, and `r+` for reading and writing. ## File Reading Methods Python provides several methods to read files in different ways according to needs. ### read() Method with Parameters ### File Position and Reading When you open a file, the file position starts from the beginning. Each reading operation advances the file position according to the amount of data read. Each reading method returns the string that was read. When reaching the end of the file, reading methods return an empty string. ## Safe File Handling Python provides the `with` statement for safer and automatic file handling. The `with` statement ensures files are always closed properly, even if an error occurs in the program. This prevents problems like files staying open and consuming system resources.