- Lab
- Data

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.

Path Info
Table of Contents
-
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, andos.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 thewrite()
method. Remember to close the file usingclose()
.π 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 theread()
method to read its contents. Remeber to close the file withfile.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, andos.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('.')
-
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 thepsutil
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. Thedisk_usage(path)
function of thepsutil
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 thepsutil
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. Thedisk_io_counters()
function of thepsutil
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 thepsutil
module to retrieve disk IO counters information.π Solution
io_counters = psutil.disk_io_counters() io_counters
-
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 thePATH
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 theos
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 thePATH
environment variable.π Hint
Use
os.environ
to access environment variables. For example, to access thePATH
variable, you would useos.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 calledGREETING
toHello, world!
.π Hint
Use
os.environ
to set the value of an environment variable. For example, to set the value ofMY_VARIABLE
, you would useos.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 ofGREETING
and print it.π Solution
os.environ['GREETING']
-
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 namedfile
.π 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 nameddata
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 namedfile
.π 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.