File handling operations

Cards (70)

  • What is file handling?
    Interaction of programs with computer files
  • What is the purpose of writing to a file?
    Save data into a file
  • What are the main operations involved in file handling?
    • Open a file: Start using a file
    • Read from a file: Get data from a file
    • Write to a file: Save data into a file
    • Close a file: Finish using a file
  • What does reading from a file allow you to do?
    Get data from a file
  • What does it mean to open a file in file handling?
    Start using a file
  • What does reading from a file involve?
    Getting information from a file
  • What does closing a file signify in file handling?
    Finish using a file
  • What is the first common file handling operation?
    Open
  • What is the second common file handling operation?
    Read
  • What does the 'Close' operation do in file handling?
    Ends file usage
  • What is the fourth common file handling operation?
    Close
  • What does the 'Write' operation do in file handling?
    Saves data to a file
  • How do you open a file for reading in Python?
    `file = open("myfile.txt", "r")`
  • How can file handling be compared to a physical object?
    Like a digital notebook for managing information
  • What function is used to open a file in Python?
    open()
  • What are the common file handling operations in Python?
    • Open: Begins using a file
    • Read: Retrieves data from a file
    • Write: Saves data to a file
    • Close: Ends file usage
  • What is the purpose of the open mode `a`?
    It adds to the end of the file
  • How would you open a file named "my_file.txt" for reading?
    file = open("my_file.txt", "r")
  • What happens when you use the open mode `x`?
    It errors if the file exists
  • What is the default open mode in Python?
    Read (`r`)
  • What does the open mode `w` do?
    It overwrites the file if it exists
  • What does the 'Read' operation do in file handling?
    Retrieves data from a file
  • What is the first step to open a file in Python?
    Specify the file path
  • What does the 'Open' operation do in file handling?
    Begins using a file
  • What is the command to close a file in Python?
    `file.close()`
  • What does the open() function return?
    A file object
  • What mode is used with `open()` to read a file?
    "r" mode
  • Why is it important to specify the file path?
    It determines where the file is located
  • How do you write to a file in Python?
    `file.write("New data")`
  • What is the command to read the entire file in Python?
    `file.read()`
  • What is the benefit of using f-strings in Python?
    They provide clear formatting for strings
  • What are the main file handling operations?
    1. Opening a file
    2. Reading from a file
    3. Writing to a file
    4. Closing a file
  • How do you open a file for writing in Python?
    Use `open("filepath", "w")`
  • How might a program use file handling in practice?
    To save a high score to a text file
  • What should you remember to do when writing to a file?
    Handle errors if the file cannot be opened
  • What should you check before closing the file in the `finally` block?
    If 'file' is in locals()
  • What will happen if the file does not exist in the `try` block?
    The program will print "File not found"
  • What is the purpose of file handling operations?
    To store and manage data in files
  • What does the `readlines()` method do?
    Reads file line by line into a list
  • What exception is caught if the file is not found?
    FileNotFoundError