Lab 10.

Our plan for today (lab 10)

Here is what we are doing to do today

  • write code to list the files and folders on our computers
  • write code to count the number of python files in a folder
  • write code analyze the content of python files in a folder

Set up

As usual, download the lab10_programs folder and change the name of the folder to be your own. There are a bunch of files in your lab10_programs folder: it should look like this:

Start Canopy. Create a new file called lab10.py in your lab10_programs folder.

Useful links:
Files and I/O lecture notes
Python tutorial reading and writing files

Task 1: Listing folders

Write a function called listFolders(folder) that will print out the list of folders in a given folder. This should work on any given folder. Note that when you run listFolders, you must specify the entire path of the folder, starting with '/Users'. Python needs to know exactly where to find the particular folder, so you have to specify it accurately. For example,
listFolders('/Users/slee/Desktop/lab10_programs')
should print out:
images
ps08_programs
because those are the names of the folders (not the files) in the lab10_programs folder. And,

listFolders('/Users/slee/Desktop/ScoobyDoo')
should print out:
Yikes!  There is no folder called ScoobyDoo.  Please try again.

Task 2: Listing python files

Write a function called listPythonFiles(folder) that will print out the list of python programs in a given folder. This should work on any given folder. All python programs must end in ".py". Hint: the built-in python function endswith() could be helpful here (look it up). This function should also handle non-existent folders without crashing.
listPythonFiles('/Users/slee/Desktop/lab10_programs')
should print out:
ConnectFour.py
cs1graphics.py
friendlyGhost.py
hangman2.py
lab10.py

Task 3: Listing and counting python files

Write a function called countAndListPythonFiles(folder) that will print out the list of python programs as well as a count of the number of python programs in the given folder. This function should also handle non-existent folders without crashing.
countAndListPythonFiles('/Users/slee/Desktop/lab10_programs')
should print out:
ConnectFour.py
cs1graphics.py
friendlyGhost.py
hangman2.py
lab10.py
A total of 5 python files in the folder /Users/slee/Desktop/lab10_programs.

Task 4: Read in a file and write a new numbered file

Write a function called addLineNumbers(filename) that will read in the given filename, and produce a new file with the original contents of the filename file, with each line numbered. If the original file is named "hello.txt", then the new numbered file will be called "helloNumbered.txt". This function should also handle non-existent files without crashing.
addLineNumbers('/Users/slee/Desktop/lab10_programs/ImSoFancy.txt')
will create a new file called ImSoFancyNumbered.txt, the contents of which will look like this:

Note it appears that the file has double numbered lines, but that is because it is viewed from within Canopy, which automatically displays line numbers. Note that nothing is printed in this function, rather, addLineNumbers() function will create a brand new file in the same folder where the original file resides. Similar results occur when invoking addLineNumbers() on, say, hangman2.py (a new file called hangman2Numbered.txt is created with each line numbered).

Task 5: Analysis of python code

  • Task 5A

    Write a function called analyzeCode(folder) that will take a look at all of the python programs in the given folder, and then print statistics on each of those python programs: 1) how many lines are comments, 2) how many lines are blank, 3) how many lines are code and 4) a total number of lines in the file. How to determine if a line is a comment? We used the criteria that the first non-empty string in the line is a "#". You might find it helpful to write a helper function, say, isCommentLine that returns True or False if a given line of the file is a comment line.
    Below is some sample output that gets printed to the screen:

    analyzeCode('/Users/sohielee/Desktop/lab10_programs')
    

    prints this out in Canopy:

  • Task 5B

    Write a function called analyzeCodeOutput(folder,outputfile) that will take ap look at all of the python programs in the given folder, and then instead of printing the various counts, will write them to a file called outputfile. So when your program is done running, a new file should be in your folder that contains the counts of different types of lines.

    analyzeCodeOutput('/Users/sohielee/Desktop/lab10_programs','pythonCounts.txt'))
    

    creates a newfile called pythonCounts.txt. You can see it listed in the contents of the lab10_programs folder (below, left). We are also showing a screenshot of the contents of pythonCounts.txt (from within Canopy) (below, right).

  • Task 5C

    Now, write another function (sometimes called a wrappper function) that allows for interaction with the user, and then calls analyzeCodeOutput(). Let's call this new function run_analysis(). run_analysis should prompt the user to type in the folder name, check that the information is indeed valid, and then proceed with calling analyzeCodeOutput() and writing the information to the new file.

    Here are some sample runs of run_analysis():

    run_analysis()
    
    produces this interaction. For illustration purposes, the user's input is shown in navy.

    Let's analyze your python programs. 
    Enter the path to a folder: ScoobyDoo
    
    The folder you entered cannot be found. Make sure the working directory is set appropriately and the path to the folder is correct.
    

    Another scenario:

    run_analysis()
    

    Let's analyze your python programs. 
    Enter the path to a folder: lab10.py
    
    Sorry, you entered the name of a file, not a folder.
    

    And yet another one:

    run_analysis()
    

    Let's analyze your python programs. 
    Enter the path to a folder: /Users/sohielee/Desktop/lab10_programs
    
    Please provide the name of the output file to be created: lab10_python
    Reading lines from file ConnectFour.py
    Reading lines from file cs1graphics.py
    Reading lines from file friendlyGhost.py
    Reading lines from file hangman2.py
    Reading lines from file lab10.py
    Data written to file lab10_python.txt.
    

    which creates the file lab10_python.txt in the /Users/slee/Desktop/lab10_programs folder, and when viewed in Canopy, it looks like this: