OS Week 7 - Lab: File Operations in Python
29 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What method would you use to check if a given path is a file in Python?

  • os.path.checkfile(file_path)
  • os.is_file(file_path)
  • os.verify_file(file_path)
  • os.path.isfile(file_path) (correct)
  • Which function returns the last modified time of a file?

  • os.file_mod_time(file_path)
  • os.path.getmtime(file_path) (correct)
  • os.access(file_path, os.LAST_MODIFIED)
  • os.last_modified(file_path)
  • If you want to create a new directory in Python, which function should be used?

  • os.mkdir(directory_name) (correct)
  • os.directory_create(directory_name)
  • os.create_dir(directory_name)
  • os.new_directory(directory_name)
  • To determine if a file is readable, which method should be applied?

    <p>os.access(file_path, os.R_OK) (A)</p> Signup and view all the answers

    What is a correct way to obtain the size of a file in Python?

    <p>os.path.getsize(file_path) (C)</p> Signup and view all the answers

    What is the main purpose of an underlying system call when reading a file?

    <p>To read from disk the metadata describing a file. (B)</p> Signup and view all the answers

    What will happen if you use the read method until the end of a file?

    <p>It will return -1 when the file is exhausted. (C)</p> Signup and view all the answers

    What is the primary effect of using the 'with' statement when opening a file?

    <p>It automatically closes the file after usage. (B)</p> Signup and view all the answers

    How should the Python code be modified to read an entire file in chunks of 100 characters?

    <p>Use a loop with file.read(100). (C)</p> Signup and view all the answers

    What type of value is returned by read calls when the file is fully read?

    <p>The value -1 indicating end of file. (B)</p> Signup and view all the answers

    What does the seek pointer do during file reading?

    <p>It maintains the current position in the file as data is read. (A)</p> Signup and view all the answers

    When using the read method multiple times, what does the last call return?

    <p>It returns the remaining characters in the file. (D)</p> Signup and view all the answers

    What will occur if the read method repeatedly reads more than available in the file?

    <p>It will read until end of file and return -1. (A)</p> Signup and view all the answers

    Which operation would you perform to create a new file in Python?

    <p>open() with mode 'w' (A)</p> Signup and view all the answers

    What is the purpose of checking if a file exists before deleting it in Python?

    <p>To avoid generating an error (C)</p> Signup and view all the answers

    What is the primary use of the 'with' statement when opening files in Python?

    <p>To ensure files are closed automatically (C)</p> Signup and view all the answers

    Which of the following is NOT a primitive file operation mentioned?

    <p>Open a file securely (A)</p> Signup and view all the answers

    What does the 'os.remove()' function do?

    <p>Deletes a specified file (D)</p> Signup and view all the answers

    How would you check if a file exists before performing an operation on it?

    <p>using os.path.exists() (C)</p> Signup and view all the answers

    Which file operation can be completed using the 'seek()' function?

    <p>Read from a specific position (C)</p> Signup and view all the answers

    What is the likely outcome if you try to delete a file that does not exist?

    <p>An error will occur (C)</p> Signup and view all the answers

    What does the 'with' statement help with when writing code?

    <p>It makes the code cleaner and manages unexpected errors better. (B), It automatically closes files after opening. (C)</p> Signup and view all the answers

    In sequential access to a file, the seek pointer begins at which byte of the file?

    <p>The byte following the last read or written byte. (B), The first byte of the file. (C)</p> Signup and view all the answers

    Which of the following statements is true regarding random access in files?

    <p>It allows you to read or write at any position in the file. (D)</p> Signup and view all the answers

    What is the purpose of the 'seek' method in file handling?

    <p>To move the seek pointer to a specific byte in the file. (B)</p> Signup and view all the answers

    How can you copy the contents of one file to another using Python?

    <p>By iterating through the source file and writing chunks to the destination file. (A)</p> Signup and view all the answers

    What happens when you use file mode 'r+b' while opening a file?

    <p>The file is opened in binary mode for both reading and writing. (B)</p> Signup and view all the answers

    Which of the following code snippets correctly writes a string to a text file?

    <p>with open('tweedle-dee.txt', 'w') as file: file.write('Hello') (C)</p> Signup and view all the answers

    What is the result of writing to a position in the file using the 'seek' method?

    <p>It replaces data at the current seek pointer location. (A)</p> Signup and view all the answers

    Study Notes

    OS Week 7 - Lab: File Primitives (Python)

    • Introduction: The lab illustrates file operations in Python, building on concepts from week 7 lecture slides on file systems. Ten primitive file operations are covered: create named file, delete named file, open named file, close a file, read from open file, write to open file, seek in open file, get attributes of file, set attributes of file, rename a file. The lab emphasizes linking these Python operations to underlying system calls using os and shutil libraries.

    Creating Files (Experiment 1)

    • Python code: Example Python code creates two text files (tweedle-dum.txt and tweedle-dee.txt) in the current directory using os module. File paths can be explicitly specified, including full paths, (e.g., C:/Users/elboghdt/Desktop/Python/).

    Deleting Files (Experiment 2)

    • Python code: Example Python code uses os.path.exists() to check if files exist before removing them with os.remove().

    Opening Files

    • Concept: Programming languages use explicit "open" calls to read/write files, either direct system calls, or system interfaces. These calls retrieve file metadata from the disk, then load this into data structures maintained by the OS.

    Reading Files (Experiment 3)

    • Purpose: Recreate tweedle-dum.txt, add text, and read the first 100 characters. Example text provided for insertion. Additional Python Example in the notes offers a way to add file content directly.

    Experiment 4: Reading Entire Files

    • Python Code Example: The lab demonstrates reading and printing an entire file's content in chunks of 100 characters, using a while loop that reads and prints content until reaching the end of the file; or just use file.read().

    Closing Files

    • Importance: The with statement automatically closes files after use, regardless of errors. This with statement approach is recommended for safer file management.

    Writing to Files (Experiment 5)

    • Purpose: Write a string to tweedle-dee.txt.

    Experiment 6: Copying File Contents

    • Content: Combines reading and writing files to copy tweedle-dum.txt data to tweedle-dee.txt

    Seeking a Files / Random Access (Experiment 7)

    • Concept: Sequential access, default for files, where the operating system pointer moves to the next byte. Random access allows reading or writing at any position in the file, regardless of prior access points.

    • Python Example: Shows code using seek to accomplish random file access.

    Experiment 8: Writing to a Random Position

    • Purpose: Demonstrates writing to a file at a specific random position

    Getting Attributes of a File (Experiment 9)

    • Purpose: Demonstrates displaying file attributes like size, last modification time.

    Additional Exercises

    • Exercises: Setting attributes, renaming files, directory manipulation (using os.mkdir and os.listdir), and recursive file copying (using shutil.copytree) are included as suggested exercises.

    Reflection Question

    • Task: Try to relate Python operations to the underlying POSIX system calls, using provided links.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    This lab focuses on file operations in Python as discussed in week 7's lecture on file systems. You will explore ten primitive file operations using the os and shutil libraries, including creating, deleting, and modifying files. Understand how these operations link back to system calls for effective file management.

    More Like This

    Automating File Operations with Python
    10 questions
    Python Programming: Units 1-4 Overview
    10 questions
    Python Programming Basics Quiz
    41 questions
    Use Quizgecko on...
    Browser
    Browser