CT320: Python
Python Programming Lab
The purpose of this assignment is to learn how write basic scripts using the Python programming language.
Documentation
- General Python documentation
- List of Python modules
- The os module
- To find out about the
math.sqrt
function:pydoc3 math.sqrt
Part 1 — Simple Python Script
Here is a program named “walk“. Your TA will discuss it.
#! /usr/bin/python3 import os for dirpath, directories, files in os.walk("."): for f in files: fpath = dirpath + '/' + f st = os.stat(fpath) mode = st.st_mode & 0o777 print("%o" % mode, fpath)
- What does
import
do? - What does
os.walk
return? - What does
0o777
mean? - What does the
%
operator do?
Part 2 — Run it
Copy & paste the script, above, to your system (beware of tabs) and make sure that it runs properly.
Part 3 — Ignore dot-files
Add code to ignore files that begin with a dot, e.g., .bashrc
.
Part 4 — ls
-like output
Add code to print the file protection bits in the style
of ls -l
, e.g., -rwxr-xr--
Part 5 — Privacy
You’re concerned about others reading your secret files. Add code to complain, to standard error, for each file that is publicly readable, writable, or executable.
Part 6 — Less privacy
Add code to not complain (see above) if the file .public
exists
in the same directory as the file that is publicly
readable/writable/executable.
Part 7 — Directories, too
Hey, we’re only listing plain files, not directories! Modify your program to also list directories.
Part 8 — Modification time
Show the modification time for the files.
Part 9 — Credit
Show your work to the TA.