Courser 2: USING PYTHON TO INTERACT WITH THE OPERATING SYSTEM

Module 2: Managing Files with Python

GOOGLE IT AUTOMATION WITH PYTHON PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

Last updated:

INTRODUCTION – Managing Files with Python

In this module, you’ll learn about reading and writing to files and the commands that will enable you to do this. We’ll learn the importance of managing files and how we can navigate through different directories. You’ll understand how to work with files and how there is a layer of abstraction between Python and the operating system. Finally, we’ll dive into learning about CSV files and how to best utilize them.

Learning Objectives

  • Read, write, and iterate through files
  • Manage files by moving, deleting, and renaming files
  • Create and navigate through directories
  • Define what CSV files are and read from them
  • Write and make edits to CSV files within directories

PRACTICE QUIZ: MANAGING FILES & DIRECTORIES

1. The create_python_script function creates a new python script in the current working directory, adds the line of comments to it declared  by the ‘comments’ variable, and returns the size of the new file. Fill in the gaps to create a script called “program.py”.

NuSpkj4mypm8MM9qyLDqZSMfPUZ5IVlkINfSCLFNzwjTxxw 7ApJQ75hmshz3364IpVQ8ma6uft3CtCZotJFm87HLfRwLc2l3 7eXYixV5pjKOXbmcCj vIJTJjpY6IRkYTVFiaXO9npL0Xst b9Zw

def create_python_script(filename):
  comments = "# Start of a new Python program"
  with open(filename, 'w') as file:
    file.write(comments)
    filesize = file.tell()
  return filesize


print(create_python_script("program.py"))

Great work! Your new python script is now ready for some real code!

2. The new_directory function creates a new directory inside the current working directory, then creates a new empty file inside the new directory, and returns the list of files in that directory. Fill in the gaps to create a file “script.py” in the directory “PythonPrograms”.

8AzShWQcAh dIIA 4 UIKFgTLaGUHmh8hiCa2uixgIrGxKNeeGDQOA64wth414b7zSMJ SGb6cDm0tPthZm6PFc4OlF q3Wua SlLC4VdXgT AZ2pQ9aYmY6Sa6HLMQkq0 wmg3i7aXvJE8S0ZiaQ

import os


def new_directory(directory, filename):
  # Before creating a new directory, check to see if it already exists
  if os.path.isdir(directory) == False:
    os.mkdir(directory)


  # Create the new file inside of the new directory
  os.chdir(directory)
  with open (filename, 'w') as file:
    pass


  # Return the list of files in the new directory
  return os.listdir()


print(new_directory("PythonPrograms", "script.py"))

Well done, you! Working with files and directories can be a little tricky, and you’re getting the hang of it!

3. Which of the following methods from the os module will create a new directory?

  • path.isdir()
  • listdir()
  • mkdir() (CORRECT)
  • chdir()

Right on! os.mkdir() will create a new directory with the name provided as a string parameter.

4. The file_date function creates a new file in the current working directory, checks the date that the file was modified, and returns just the date portion of the timestamp in the format of yyyy-mm-dd. Fill in the gaps to create a file called “newfile.txt” and check the date that it was modified.

E0yvmww8JjcqPfbfIjN8V0N163uqNGmoxNud2Bf yCDyxQCGU37Ok2c3GyWQLLnLkDPKVHhVG114miIBTevioW6Ng QKqyu6hYt U57HdDoeXa6Y4kILlO2GzL3VE vMKVYVqbuAzlqk5F1q2CiEfQ

import os
import datetime


def file_date(filename):
  # Create the file in the current directory
  with open(filename, 'w') as file:
    pass
  
  # Get the timestamp of when the file was last modified
  timestamp = os.path.getmtime(filename)


  # Convert the timestamp into a readable format, then into a string
  date_modified = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d')


  # Return just the date portion 
  # Hint: how many characters are in “yyyy-mm-dd”? 
  return ("{}".format(date_modified))


print(file_date("newfile.txt")) 
# Should be today's date in the format of yyyy-mm-dd

Way to go! You remembered the commands to convert timestamp and format strings, to get the results that were requested.

5. The parent_directory function returns the name of the directory that’s located just above the current working directory. Remember that ‘..’ is a relative path alias that means “go up to the parent directory”. Fill in the gaps to complete this function.

25NWNQlEHevcF68kvVZafOu6k1AwK860wem w ReqJkA8bqLDW62PfIV2Io3KwSUV3tWLxR FAbSQ2sDLBJ3WWJxhC8TxZqIEJUzRL7tKYyB7SiXFIeIqXs ECkIVY5 2ZQ0F 8ZoVGslbHjXFmZiA

import os


def parent_directory():
  # Create a relative path to the parent 
  # of the current working directory 
  relative_parent = os.path.join(os.getcwd(), '..')


  # Return the absolute path of the parent directory
  return os.path.abspath(relative_parent)


print(parent_directory())

Excellent! You made all the right moves to print the path of the parent directory!

6. What is the difference between the readline() and read() methods?

  • The readline() method starts from the current position, while the read() method reads the whole file.
  • The read() method reads a single line, the readline() method reads the whole file.
  • The readline() method reads the first line of the file, the read() method reads the whole file.
  • The readline() method reads a single line from the current position, the read() method reads from the current position until the end of the file. (CORRECT)

Right on! Both methods read from the current position. The readline() method reads one line, while read() reads until the end of the file.

7. Can you identify which code snippet will correctly open a file and print lines one by one without whitespace?

with open("hello_world.txt") as text:
    for line in text:
        print(line)
with open("hello_world.txt") as text:
    for line in text:
        print(text)
with open("hello_world.txt") as text:
    print(line)
with open("hello_world.txt") as text:
    for line in text:
        print(line.strip()) (CORRECT)

Good work! Here, we are iterating line by line, and the strip() command is used to remove extra whitespace.

8. What happens to the previous contents of a file when we open it using “w” (“write” mode)?

  • The new contents get added after the old contents.
  • A new file is created and the old contents are kept in a copy.
  • The old contents get deleted as soon as we open the file. (CORRECT)
  • The old contents get deleted after we close the file.

You nailed it! When using write mode, the old contents get deleted as soon as the file is opened.

9. How can we check if a file exists inside a Python script?

  • Renaming the file with os.rename.
  • Creating the file with os.create.
  • Using the os.path.exists function.  (CORRECT)
  • Deleting the file with os.remove.

You got it! The os.path.exists function will return True if the file exists, False if it doesn’t.

10. Some more functions of the os.path module include getsize() and isfile() which get information on the file size and determine if a file exists, respectively. In the following code snippet, what do you think will print if the file does not exist?

import os
file= "file.dat"
if os.path.isfile(file):
    print(os.path.isfile(file))
    print(os.path.getsize(file))
else:
    print(os.path.isfile(file))
    print("File not found")
file.dat
1024
False
2048
True
512
False
File not Found (CORRECT)

Awesome! Because the file does not exist, getsize() will never be called and our error message will be printed instead.

11. What’s the purpose of the os.path.join function?

  • It creates a string containing cross-platform concatenated directories. (CORRECT)
  • It creates new directories.
  • It lists the file contents of a directory.
  • It returns the current directory.

Right on! By using os.path.join we can concatenate directories in a way that can be used with other os.path() functions.

PRACTICE QUIZ: READING & WRITING CSV FILES

1. We’re working with a list of flowers and some information about each one. The create_file function writes this information to a CSV file. The contents_of_file function reads this file into records and returns the information in a nicely formatted block. Fill in the gaps of the contents_of_file function to turn the data in the CSV file into a dictionary using DictReader.

d9SH9YnkB0Gig6PG56KluY6juJUnATadKXHMW9BYSICj6u5v84KRGKjg5pAt6aYzhrwc Ho4sydbZcpnJg63VRChyBz3nSAKR30 rtW9NWweh8z8mG Oe SUn4UzHBP nXqZRAAitqoSKO3ROf8rQ

import os
import csv


# Create a file with data in it
def create_file(filename):
  with open(filename, "w") as file:
    file.write("name,color,type\n")
    file.write("carnation,pink,annual\n")
    file.write("daffodil,yellow,perennial\n")
    file.write("iris,blue,perennial\n")
    file.write("poinsettia,red,perennial\n")
    file.write("sunflower,yellow,annual\n")


# Read the file contents and format the information about each row
def contents_of_file(filename):
  return_string = ""


  # Call the function to create the file 
  create_file(filename)


  # Open the file
  with open(filename, mode='r') as file:
    # Read the rows of the file into a dictionary
    reader = csv.DictReader(file)
    
    # Process each item of the dictionary
    for row in reader:
      return_string += "a {} {} is {}\n".format(row["color"], row["name"], row["type"])
  return return_string


# Call the function
print(contents_of_file("flowers.csv"))

Well done! Your garden of Python skills is really blooming!

2. Using the CSV file of flowers again, fill in the gaps of the contents_of_file function to process the data without turning it into a dictionary. How do you skip over the header record with the field names?

import os
import csv


# Create a file with data in it
def create_file(filename):
  with open(filename, "w") as file:
    file.write("name,color,type\n")
    file.write("carnation,pink,annual\n")
    file.write("daffodil,yellow,perennial\n")
    file.write("iris,blue,perennial\n")
    file.write("poinsettia,red,perennial\n")
    file.write("sunflower,yellow,annual\n")


# Read the file contents and format the information about each row
def contents_of_file(filename):
  return_string = ""


  # Call the function to create the file 
  create_file(filename)


  # Open the file
  with open(filename, mode='r') as file:
    # Read the rows of the file
    rows = csv.reader(file)
    
    # Process each row
    for row in rows:
      # Skip over the header record with field names
      if row[0] == "name":
          continue
      
      # Format the return string for data rows only
      return_string += "a {} {} is {}\n".format(row[1], row[0], row[2])
  return return_string


# Call the function
print(contents_of_file("flowers.csv"))

You nailed it! Everything’s coming up roses (pardon the pun!)

3. In order to use the writerows() function of DictWriter() to write a list of dictionaries to each line of a CSV file, what steps should we take? (Check all that apply)

  • Create an instance of the DictWriter() class (CORRECT)
  • Write the fieldnames parameter into the first row using writeheader() (CORRECT)
  • Open the csv file using with open (CORRECT)
  • Import the OS module

Excellent! We have to create a DictWriter() object instance to work with, and pass to it the fieldnames parameter defined as a list of keys.

Nice work! The non-optional fieldnames parameter list values should be written to the first row.

Good call! The CSV file has to be open before we can write to it.

4. Which of the following is true about unpacking values into variables when reading rows of a CSV file? (Check all that apply)

  • We need the same amount of variables as there are columns of data in the CSV (CORRECT)
  • Rows can be read using both csv.reader and csv.DictReader (CORRECT)
  • An instance of the reader class must be created first (CORRECT)
  • The CSV file does not have to be explicitly opened

Awesome! We need to have the exact same amount of variables on the left side of the equals sign as the length of the sequence on the right side when unpacking rows into individual variables.

Right on! Although they read the CSV rows into different datatypes, both csv.reader or csv.DictReader can be used to parse CSV files.

Nice job! We have to create an instance of the reader class we are using before we can parse the CSV file.

5. If we are analyzing a file’s contents to correctly structure its data, what action are we performing on the file?

  • Writing
  • Appending
  • Parsing (CORRECT)
  • Reading

Great work! Parsing a file means analyzing its contents to correctly structure the data. As long as we know what the data is, we can organize it in a way our script can use effectively.

6. If we have data in a format we understand, then we have what we need to parse the information from the file. What does parsing really mean?

  • Using rules to understand a file or datastream as structured data. (CORRECT)
  • Uploading a file to a remote server for later use, categorized by format
  • Writing data to a file in a format that can be easily read later
  • Reducing the logical size of a file to decrease disk space used and increase network transmission speed.

Right on! If we know the format of the data, we can separate it into understandable parts.

7. Which of the following lines would correctly interpret a CSV file called “file” using the CSV module? Assume that the CSV module has already been imported.

  • file.opencsv()
  • data=file.csv()
  • data=csv.reader(file) (CORRECT)
  • data=csv.open(file)

Right on! The reader() function of the CSV module will interpret the file as a CSV.

8. Which of the following must we do before using the csv.writer() function?

  • Import the functools module.
  • Open the file with write permissions. (CORRECT)
  • Import the argparse module.
  • Open the file with read permissions.

Nice work! The file must be open, preferably using with open() as, and write permissions must be given.

9. DictReader() allows us to convert the data in a CSV file into a standard dictionary. DictWriter() \ allows us to write data from a dictionary into a CSV file. What’s one parameter we must pass in order for DictWriter() to write our dictionary to CSV format?

  • The DictReader() function must be passed the CSV file
  • The writerows() function requires a list of key
  • The writeheader() function requires a list of keys
  • The fieldnames parameter of DictWriter() requires a list of keys (CORRECT)

Right on! This will help DictWriter() organize the CSV rows properly.

CONCLUSION – Managing Files with Python

In conclusion, this module has equipped you with valuable insights into the fundamental processes of reading from and writing to files, offering a comprehensive understanding of the essential commands involved. The emphasis on effective file management and navigation across different directories underscores the importance of organized data handling. You now possess a clear comprehension of the intricate mechanics involved in working with files, including the pivotal layer of abstraction between Python and the operating system.

Furthermore, the in-depth exploration of CSV files has provided you with practical knowledge and optimal strategies for their efficient utilization. Armed with these skills, you are well-prepared to manage and manipulate data seamlessly in your Python projects.