Guide to High School Computer Science
  • 💻Introduction
    • windows & Python Development
    • macOS & Python Development
    • Visual Studio Code Settings
    • Set up Github
    • Author Page
  • 🧠Prerequisite Skills
    • Keyboard Typing
    • Files & Directories
    • Use of Command Line
    • Git & GitHub
    • Markdown
    • Starting Your Python Project
  • 🐍Python Programming
    • 🍎Python Basics
      • What is Python?
      • Procedural Programming & Programming Paradigms
      • String Formatting
      • Data Types
      • Input & Output to Console
      • Working with Numbers
      • Useful Built-in Functions
      • Math & Random Module
      • Boolean Data Object
      • Comparison, Logical, and Membership Operators
      • If Statements
      • Binary Decisions
      • Multiple Decisions
      • Nested Conditions
      • [EXTRA] Bitwise Operators
      • [EXTRA] Python Style Guide
    • ⏮️Iterations
      • Introduction to While Loops
      • Infinite Loop
      • Controlling Your While Loops
      • Introduction to For Loops
      • For Loops w/ Numeric Sequences
      • For Loops w/ Strings & Lists
      • Iterable Functions w/ For Loops
    • 📦Collections
      • Strings
        • String Basics
        • String Indexing
        • String Slicing
        • String Operators & Functions
        • Basic String Methods
        • String Methods Extended
        • String Methods Document
      • Tuples & Lists
        • Tuples
        • List Basics
        • List are Mutable
        • Adding Items to a List
        • Removing Items from a List
        • Search & Reverse a List
        • List Comprehension
        • List Methods Document
      • Sets
      • Dictionary
      • How to Store Multiple Data Items
    • 💡Defining Functions
      • Functions
      • print() vs return
      • Pre-determined Arguments
      • Nested Functions
      • Map & Filter
      • [Extra] Dynamic Arguments
    • 💾File I/O
      • How to Save Text to an External File
      • Reading CSV in Python
      • Reading JSON in Python
    • 🔨Basic Python Projects
      • Basic Calculator
        • Improving the calculator
        • Exercise Set 1
        • Exercise Set 2
        • 💎Streamlit Application #1
      • Basic Password Generator
        • Exercise Set 3
        • Exercises Related to Math
        • 💎Streamlit Application #2
      • A To-Do Task List
    • ⏳Introduction to Algorithmic Thinking
      • Big-O Notation
      • Basic Algorithms
        • Linear Search
        • Binary Search
        • Basic Sorting Algorithms
      • Recursion
      • Brute Force Algorithms
      • Greedy Algorithm
        • Time on Task (CCC 2013 J4)
        • Dijkstra’s Algorithm
      • Divide and Conquer
        • Merge Sort
      • Dynamic Programming
    • 🤯Object Oriented Programming
      • Class & Objects (Definitions)
      • OOP in Python
      • Encapsulation
      • Polymorphism
      • Inheritance & Overriding
      • Override Magic Methods
      • Case Study: 2D Vectors
      • Case Study: Deck of Cards
      • Exercise
      • Abstract Data Types
      • Case Study: Static 1D Array From Java
    • Competitive Programming
      • Is This Sum Possible?
        • Is the dataset sorted?
        • Searching for a value
        • Determine if the difference between an integer from the array and the target value exists
        • Sorting Algorithms
        • Using Two Pointers
      • Two Sum - LeetCode
        • Generate all possible pairs of values
        • Subtract each value from the target, see if the difference exists in the list
      • Longest Common Prefix - LeetCode
        • Compare all possible prefixes
        • Create the longest common prefix with the direct neighbour
      • Length of Last Word - LeetCode
        • Compare all possible prefixes
      • Where can I go from one point to another?
      • Sample Outline
    • IB Recipe Book
  • 💾Python & Databases
    • Intro to Databases & Data Modeling
      • Common Data Types in SQL
      • Introduction to ERDs
      • Primary Keys and Foreign Keys
      • Database Normalization
    • What is SQL?
      • Getting Started
      • SELECT Queries
        • Selection with Conditions
        • Selection with Fuzziness
        • Selection and Sorting in Order
        • Selection without Duplicates
        • Selection with Limited Number of Outputs
      • AGGREGATE Queries
        • Counting Rows
        • Sum, Average, Min/Max Queries
        • Working with Aggregate Queries
        • Power of using Groups
        • Exercise
      • Interacting with Multiple Table
      • Inserting Data
      • External Resource
  • ☕Java Essentials
    • Basics
      • Starting Java
      • Data & Variables
      • Handling User Inputs & Type Conversion
      • Arithmetic
      • IPO Model
      • Basic Built-in Methods
      • Exercise Questions
    • Conditionals
      • Boolean Operators
      • Compare Strings
      • If Statements
      • If Else Statements
      • Making Multiple Decisions
      • Using Switch
      • Flowchart Symbols
      • Exercise Questions
    • Iterations
      • While Loops
      • For Loop
      • Exercises
    • Java Type Casting
    • Strings
      • Common String Practices
      • String Formatting
      • Java Special Characters
    • Collection
      • Arrays
      • For Each Loop
      • ArrayList
      • Exercise Questions
    • Static Methods
      • (Aside) Clearing your Console
    • Randomness in Java
    • Delayed Output in Java
    • Java Output Formatting
    • Java Style Guide
  • 🛠️JavaScript Programming
    • Our Programming Editor & Workflow
      • Hello, world!
      • Commenting & Variables
      • Data in JavaScript
      • Operators
      • String Formatting
      • Getting User Input
    • JavaScript Exercise Set 1
    • Making Decisions
      • Comparing Values
      • Combining Boolean Comparisons
      • Creating Branches
    • JavaScript Exercise Set 2
    • While Loops
      • Infinite While Loop
      • While Loops and Numbers
      • While Loops and Flags
      • While loops w/ Strings
    • JavaScript Exercise Set 3
    • Subprograms & Functions
      • Creating a Function in JavaScript
      • Function with Input and Assignable Output
    • JavaScript Exercise Set 4
  • 💾Topics in CS
    • Computer Environments & Systems
      • Computer Components
        • In-depth Explanations
      • File Maintenance
      • Computer & Safety
      • Software Development
      • Bits & Binary
    • Careers related to Computer Science
    • Postsecondary Opportunities
Powered by GitBook
On this page
  • Benefits
  • Example: Reading a .txt file
  • How to do File I/O in Python
  • Note
  • open() different modes
  • How to read data from a file.
  • How to write new data onto a file.
  1. Python Programming

File I/O

Previous[Extra] Dynamic ArgumentsNextHow to Save Text to an External File

Last updated 5 months ago

File Input / Output (I/O) is a fundamental concept in programming that involves reading data from and writing data to external files.

It is recommended that you read the following article from .

Benefits

  1. Data Persistence: File I/O allows programs to store data persistently on disk, ensuring information is retained across program runs.

  2. Data Sharing: It provides a means for different programs and platforms to exchange data efficiently.

  3. Configuration and Logging: File I/O is essential for storing program configurations and logging events and errors.

  4. Data Import/Export: Files enable data import/export in various formats, making it valuable for data analysis and interactivity.

  5. Resource Management and Security: Properly managing file resources is essential, and file permissions ensure data security from unauthorized access.

In Python, the open() function can read plain text files without the need to import additional modules. You can read files with the following extensions using open() without importing any additional modules:

  1. .txt: Plain text files.

  2. .log: Log files.

  3. .ini: INI configuration files.

  4. .cfg, .conf: Configuration files.

  5. .dat: Generic data files.

For these file types, you can use open() to read the content line by line or as a whole, depending on your needs.

Example: Reading a .txt file

# Open and read a plain text file
with open('example.txt', 'r') as file:
    content = file.read()
    print(content)

How to do File I/O in Python

# Formatting

with open('path/filename.extension', 'mode') as file_object_label:
    
    # Some code here dealing with the file object

# end of open() ... the file is now closed.

Note

  • open() creates a file object in Python that links an external file to your python script

  • with open() as label: creates a new code block where you can interact with the file object created by open().

    • We use this nomenclature to let the with statement close our files if we exit the code block.

    • Closing a file object in Python is important because it releases system resources (such as file handles) associated with the file, ensuring that other programs can access it. Failing to close a file properly may lead to resource leaks and potential data corruption, especially when writing to a file, as data may not be flushed from memory to disk until the file is closed.

open() different modes

# Reading Data
with open("example1.txt" , "r") as file_obj:

In this situation, we set our mode as "r" for reading. Our intended purpose with the file is to obtain data from the file.

# Writing Data
with open("example2.txt" , "w") as file_obj:

In this situation, we set our mode as "w" for reading. Our intended purpose with the file is to overwrite an existing data from the file.

If the "example2.txt" file did not exist in the same directory as the python script, it will create a new file entitled "example2.txt".

# Appending Data
with open("example.txt" , "a") as file_obj:

In this situation, we set our mode as "a" for appending. Our intended purpose with the file is to add data starting from the end of the file. If the file does not exist, it will be created.

Other Notable Modes:

'x' (Exclusive Creation):

  • Opens the file for exclusive creation.

  • Raises an error if the file already exists.

'+' (Read/Write):

  • Appends '+' to any mode ('r+', 'w+', 'a+', etc.) to allow both reading and writing.

  • The file is opened in both read and write modes.

How to read data from a file.

  1. .read():

    • This method reads the entire content of the file as a single string.

    • You can specify the number of characters to read as an optional argument, but if omitted, it reads the entire file.

    with open('example.txt', 'r') as file:
        content = file.read()  # Read the entire content
        print(content)
  2. .readline():

    • This method reads a single line from the file each time it's called.

    • It returns an empty string when it reaches the end of the file.

    with open('example.txt', 'r') as file:
        line1 = file.readline()  # Read the first line
        line2 = file.readline()  # Read the second line
        print(line1)
        print(line2)
  3. .readlines():

    • This method reads all lines of the file and returns them as a list of strings.

    • Each element in the list represents a line from the file.

    with open('example.txt', 'r') as file:
        lines = file.readlines()  # Read all lines
        for line in lines:
            print(line)

It's important to note that these methods maintain a position in the file, so consecutive calls to .readline() will read successive lines, and calling .read() or .readlines() again will continue from where the file pointer is located. To reset the file pointer, you can use .seek() to change the file's position.

How to write new data onto a file.

  1. Open the File in Write Mode: First, you need to open the file in write mode ('w'). If the file doesn't exist, it will be created. If it does exist, its contents will be truncated (deleted).

    with open('example.txt', 'w') as file:
        # Use 'w' mode to open the file for writing
        # This will create a new file or overwrite an existing one
  2. Use .write() to Write Data: You can use the .write() method to write data to the file. It takes a single string argument that represents the data you want to write.

    with open('example.txt', 'w') as file:
        file.write("Hello, world!\n")
        file.write("This is a new line of text.")
  3. Close the File: After writing data, it's a good practice to close the file using the with statement or the .close() method to ensure that changes are flushed to the file and resources are released.

    with open('example.txt', 'w') as file:
        file.write("Hello, world!\n")
        file.write("This is a new line of text.")
    # File is automatically closed when the 'with' block exits

This code will create or overwrite the file "example.txt" with the specified text. If the file already exists, the content will be replaced, so be cautious when using 'w' mode to avoid unintentional data loss.

To avoid deleting the contents of a file and add on data instead, the mode should be set to "a" for your open() mode.

🐍
💾
realpython