In: Computer Science
Instructions
In this exercise, you will use Python to complete four practical challenges:
Class Notes Folder
Create a script called create_notes_drs.py. In the file, define and call a function called main that does the following:
Bonus Challenge: Add a conditional statement to abort the script if the directory CyberSecurity-Notes already exists.
Copying Student Exercises
So far you've used a few different Python modules, but for the rest of the homework, you will need to familiarize yourself with a new one. The shutil module is a Python module used for high-level file operations like moving and copying. Read this beforehand to get familiar with shutil and make sure to use the documentation while you're working through the homework.
Create a script called copy_activities.py with a function called stu_activities that does the following:
Note: This isn't just a challenge to complete for the sake of it, this is a practical script you can run to move any downloaded files from class into your class notes directories.
Copy Class Slides
Create a script called copy_slides.py with a function called pptx_copy Students will create a script that does the following:
Note: This is another practical script you can use to move downloaded slides from class into your class notes directories.
Updating PATH and Add an Alias
Note: Consider this a bonus. You do not need to complete this step for credit. But, these tools will come up in class later, so you're strongly encouraged to study up now!
Now these great scripts have been written, but they are only executable from their relative path - where the files are in your system. In this final step, we'll make them accessible to you anywhere in your system directory.
export PATH=$PATH:/usr/local/bin
What is the main difference betweeen ~/.bashrc and the
~/.bash_profile?
.bash_profile is executed for login shells, while .bashrc is
executed for interactive non-login shells.
What does the export PATH command do?
Export command marks an environment variable to be exported with
any newly forked child processes and
thus it allows a child process to inherit all marked
variables.
What is the benefit of creating aliases?
• It eliminates the need to manage multiple email addresses for a
single website.
• With it, you don’t need to create new email addresses.
Add scripts to bash file.
create_notes_drs.py
import os, errno
def createDirectory(path, k=24):
week = "Week"
for w in range(1, k+1):
weekPath =
path+"/"+week+" "+str(w)
os.makedirs(weekPath,
exist_ok=True)
for d in range(1,
4):
day = "Day"
os.makedirs(weekPath+"/"+day+" "+str(d), exist_ok=True)
def main():
directory = "CyberSecurity-Notes"
dir = os.path.exists(directory)
if not dir:
try:
os.makedirs(directory)
createDirectory(directory)
except OSError as
e:
if e.errno != errno.EEXIST:
raise Exception("unable to create dir")
else:
raise
Exception("Directory already exist!")
main()
copy_activities.py
import os, fnmatch
import shutil
def stu_activities():
downloadsPath =
os.path.os.path.expanduser("~")+"/Downloads/"
ls = fnmatch.filter(os.listdir(downloadsPath),
'Stu_*')
for i in ls:
shutil.copyfile(downloadsPath+"/"+i, "./"+i)
stu_activities()
copy_slides.py
import os, fnmatch
import shutil
def pptx_copy():
downloadsPath =
os.path.os.path.expanduser("~")+"/Downloads/"
lsPDF =
fnmatch.filter(os.listdir(downloadsPath), '*.pptx')
lsPEM =
fnmatch.filter(os.listdir(downloadsPath), '*.ppt')
totalFiles = lsPDF+lsPEM
for i in totalFiles:
shutil.copyfile(downloadsPath+"/"+i, "./"+i)
return
pptx_copy()