Hamburger Icon
  • Labs icon Lab
  • Data
Labs

Working with Files in Python Hands-on Practice

In this lab, you'll learn to manage files and directories, handle system processes with psutil, manipulate environment variables, and perform read/append operations on CSV files, gaining essential skills in Python file handling.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 36m
Published
Clock icon Dec 06, 2023

Contact sales

By filling out this form and clicking submit, you acknowledge ourΒ privacy policy.

Table of Contents

  1. Challenge

    Working with Directories and Files

    Jupyter Guide

    To get started, open the file on the right entitled "Step 1...". You'll complete each task for Step 1 in that Jupyter Notebook file. Remember, you must run the cells (ctrl/cmd + Enter) for each task before moving onto the next task in the Jupyter Notebook. Continue until you have completed all tasks in this step. Then when you are ready to move onto the next step, you'll come back and click on the file for the next step until you have completed all tasks in all steps of the lab.


    Working with Directories and Files

    To review the concepts covered in this step, please refer to the Creating, Reading, Writing, Deleting, and Managing Files and Directories module of the Working with Files in Python course.

    Understanding how to work with directories and files is important because it forms the basis of file handling in Python. This includes creating, reading, writing, and deleting files and directories.

    In this step, you'll practice these skills by creating, reading, writing, and deleting files and directories using Python's os module. Your task is to create a new directory, write a file within it, read the file, and finally delete both the file and the directory.


    Task 1.1: Creating a New Directory

    Create a new directory named 'LabFiles' in the current working directory using the os module. After creating the directory, list the contents of the current directory to confirm its creation.

    πŸ” Hint

    Use the os.mkdir() function to create a new directory, and os.listdir() to list the contents of a directory.

    πŸ”‘ Solution
    import os
    
    # Create a new directory named 'LabFiles'
    os.mkdir('LabFiles')
    
    # Print the list of files/directories in the current directory
    os.listdir('.')
    

    Task 1.2: Writing a File

    Create a new file named 'demographic_data.txt' in the 'LabFiles' directory and write the string 'This is a test file for demographic data.' to it. After writing to the file, close it and list the contents of the 'LabFiles' directory to confirm the file's creation.

    πŸ” Hint

    To create and write to a file, use the open() function with 'w' mode, and the write() method. Remember to close the file using close().

    πŸ”‘ Solution
    file = open('LabFiles/demographic_data.txt', 'w')
    file.write('This is a test file for demographic data.')
    file.close()
    
    # Print the list of files in the 'LabFiles' directory
    os.listdir('LabFiles')
    

    Task 1.3: Reading a File

    Read the contents of the 'demographic_data.txt' file and print them. After reading the file, close it to ensure resources are released properly.

    πŸ” Hint

    Use the open() function with 'r' mode to open the file for reading. Utilize the read() method to read its contents. Remeber to close the file with file.close()!

    πŸ”‘ Solution
    file = open('LabFiles/demographic_data.txt', 'r')
    data = file.read()
    file.close()
    
    # Print the contents of 'demographic_data.txt'
    data
    

    Task 1.4: Deleting a File

    Delete the 'demographic_data.txt' file from the 'LabFiles' directory. After deleting the file, list the contents of the 'LabFiles' directory to verify its removal. There should be nothing there.

    πŸ” Hint

    Use the os.remove() function to delete a file, and os.listdir() to list the contents of a directory.

    πŸ”‘ Solution
    os.remove('LabFiles/demographic_data.txt')
    
    # Print the list of files in the 'LabFiles' directory to confirm deletion
    os.listdir('LabFiles')
    

    Task 1.5: Deleting a Directory

    Delete the 'LabFiles' directory. After deleting it, list the contents of the current directory to verify the directory's removal.

    πŸ” Hint

    Use the os.rmdir() function to delete a directory.

    πŸ”‘ Solution
    os.rmdir('LabFiles')
    
    # Print the list of files/directories in the current directory to confirm deletion
    os.listdir('.')
    
  2. Challenge

    Interacting with System Processes

    Interacting with System Processes

    To review the concepts covered in this step, please refer to the Creating, Reading, Writing, Deleting, and Managing Files and Directories module of the Working with Files in Python course.

    Knowing how to interact with system processes is important because it allows you to manage and retrieve information from running processes, which is crucial in many programming tasks such as monitoring system performance, debugging, and automating system maintenance tasks.

    Time to interact with your system! In this step, you'll use the psutil module to retrieve system information and interact with running processes. Your goal is to retrieve information related to files, such as disk partitions, disk usage, and disk IO counters, and to print all running processes on your machine in a human-readable format.


    Task 2.1: Importing the psutil module

    To interact with system processes, you need to import the psutil module. This module provides an interface for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python.

    πŸ” Hint

    Use the import keyword to import the psutil module.

    πŸ”‘ Solution
    import psutil
    

    Task 2.2: Printing All Running Processes

    Print all running processes on your machine. This is useful for monitoring which processes are currently active, understanding system resource usage, and identifying potentially unwanted or suspicious processes. Find the jupyter-notebook process that you are using at the moment.

    πŸ” Hint

    Use the psutil.process_iter() function to iterate over all running processes. Pass it a list of strings representing the data about each process you want to retrieve. For example:

    for process in psutil.process_iter(['pid', 'name', 'memory_info']):
         # access these attributes in the process.info dictionary
    
    πŸ”‘ Solution
    for process in psutil.process_iter(['pid', 'name', 'memory_info']):
        print(f"PID: {process.info['pid']}, Name: {process.info['name']}, Memory: {process.info['memory_info']}")
    

    Task 2.3: Retrieving Disk Usage Information

    You can also use the psutil module to retrieve information about the disk usage of your system. The disk_usage(path) function of the psutil module returns disk usage statistics about the given path as a named tuple including total, used and free space expressed in bytes, plus the percentage of used space.

    πŸ” Hint

    Use the disk_usage(path) function of the psutil module to retrieve disk usage information. You can use '/' as the path to get the disk usage of the root directory.

    πŸ”‘ Solution
    usage = psutil.disk_usage('/')
    usage
    

    Task 2.4: Retrieving Disk IO Counters Information

    Finally, you can use the psutil module to retrieve information about the disk IO counters of your system. The disk_io_counters() function of the psutil module returns system-wide disk IO statistics as a named tuple including the number of read and write calls, the amount of data read and written, the time spent reading and writing, and the number of bytes read and written.

    πŸ” Hint

    Use the disk_io_counters() function of the psutil module to retrieve disk IO counters information.

    πŸ”‘ Solution
    io_counters = psutil.disk_io_counters()
    io_counters
    
  3. Challenge

    Working with Environment Variables

    Working with Environment Variables

    To review the concepts covered in this step, please refer to the Creating, Reading, Writing, Deleting, and Managing Files and Directories module of the Working with Files in Python course.

    Working with environment variables is important because it allows you to access and manipulate data that your application might need, regardless of the platform it's running on.

    Let's explore the environment! In this step, you'll use the os module to retrieve and work with environment variables. Your goal is to retrieve the PATH environment variable and add a new environment variable.


    Task 3.1: Importing the os module

    To start working with environment variables, you need to import the os module. This module provides a way of using operating system dependent functionality.

    πŸ” Hint

    Use the import keyword to import the os module.

    πŸ”‘ Solution
    import os
    

    Task 3.2: Accessing an environment variable

    Now that you have imported the os module, you can use it to access environment variables. Try to access the PATH environment variable.

    πŸ” Hint

    Use os.environ to access environment variables. For example, to access the PATH variable, you would use os.environ['PATH'].

    πŸ”‘ Solution
    os.environ['PATH']
    

    Task 3.3: Setting an environment variable

    You can also set the value of an environment variable using the os module. Try setting the value of a new environment variable called GREETING to Hello, world!.

    πŸ” Hint

    Use os.environ to set the value of an environment variable. For example, to set the value of MY_VARIABLE, you would use os.environ['MY_VARIABLE'] = 'my text'.

    πŸ”‘ Solution
    os.environ['GREETING'] = 'Hello, world!'
    

    Task 3.4: Verifying the environment variable

    Now that you have set the value of GREETING, verify that it has been set correctly by printing its value.

    πŸ” Hint

    Use os.environ to access the value of GREETING and print it.

    πŸ”‘ Solution
    os.environ['GREETING']
    
  4. Challenge

    Reading and Writing to Files

    Reading and Appending to CSV Files

    To review the concepts covered in this step, please refer to the Creating, Reading, Writing, Deleting, and Managing Files and Directories module of the Working with Files in Python course.

    Reading and writing to files is important because it's a fundamental way to store and retrieve data in your Python applications.

    Ready to read and write? In this step, you'll open a file, append data to it and read lines from it using Python's built-in file handling functions. We'll be using the 'Demographic Data.csv' file.


    Task 4.1: Open the File

    Open the 'Demographic_Data.csv' file in the current directory in read mode using Python's built-in open() function. Assign the file object to a variable named file.

    πŸ” Hint

    Use the open() function with the file path and the mode as 'r' for read mode.

    πŸ”‘ Solution
    file = open('Demographic_Data.csv', 'r')
    

    Task 4.2: Read the File

    Read the contents of the file using the read() method of the file object. Assign the result to a variable named data and print the results.

    πŸ” Hint

    Use the read() method of the file object to read its contents.

    πŸ”‘ Solution
    data = file.read()
    print(data)
    

    Task 4.3: Close the File

    Close the file using the close() method of the file object.

    πŸ” Hint

    Use the close() method of the file object to close the file.

    πŸ”‘ Solution
    file.close()
    

    Task 4.4: Open the File in Append Mode

    Open the 'Demographic_Data.csv' file in append mode using Python's built-in open() function. Assign the file object to a variable named file.

    πŸ” Hint

    Use the open() function with the file path and the mode as 'a' for append mode.

    πŸ”‘ Solution
    file = open('Demographic_Data.csv', 'a')
    

    Task 4.5: Write to the File

    Write a new line to the file using the write() method of the file object. The new line should be a string representing a new employee's data. Remember to include a newline character (\n) at the end of the string. The string should be in the format: gender, date of birth, country.

    πŸ” Hint

    Use the write() method of the file object to write a new line to the file. The new line should be a string in the format of the existing data, followed by a newline character (\n).

    πŸ”‘ Solution
    file.write('Male,1990-01-01,USA\n')
    

    Task 4.6: Close the File

    Close the file using the close() method of the file object. Access the file in the file tree of the jupyter UI to verify that your changes were made.

    πŸ” Hint

    Use the close() method of the file object to close the file.

    πŸ”‘ Solution
    file.close()
    

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.