With Python: For this part of the lab, you will create a python program (recursi
ID: 3827239 • Letter: W
Question
With Python:
For this part of the lab, you will create a python program (recursive dir traversal.py) that will allow the user to provide the path of a directory to your program as a command line parameter. You have to display the number of python files and the total number of files in that directory, its subdirectories, their subdirectories, and so on. For example, the user may type the following at the terminal python 3 recursive dir traversal.py /home/alark/csl 10/labs Your program must do the following: Read in the command line parameters and get the user-provided path Write a recursive function that takes in the path as the input In the recursive function, you have to decide whether the contents of the path provided in the form of a string are a file or a directory. For example, if the user provides the path as /home/alark/cs110/labs, lab1/labl.py, then the provided 'path' is actually a file. Alternatively, /home/alark/cs110/labs/labl is a directory. You can use oss path .isfile (path) in your program to identify whether a path is a file or a directory. If the path is a file, then check if the file is a python file. Use a string function for this operation. Increment the counterfor python files. If the file is not a python file, then increment the count for all files since you need to keep a track of the total number of files in the directory and all its subdirectories If the path is a directory, then obtain a list of all the files in that directory and call the recursive function on everyelement in that list. Given a path you can obtain the list of all the files in that directory using files oss. listdir (path) For each Python file found, write a function called countLines (path) that is passed in the path to that Python file. In the function, open the Python file in read mode, count the number of lines in that file, and return that number. At the end of the program, you must display the total number of python files found and the average number of lines for all the Python files in all the directories and subdirectoriesExplanation / Answer
To count files and directories non-recursively you can use os.listdir and take its length.
To count files and directories recursively you can use os.walk to iterate over the files and subdirectories in the directory.
If you only want to count files not directories you can use os.listdir and os.path.file to check if each entry is a file:
This will print out the lines of each file in the pyfiles[]. You can also pass any mode.
Countline(path)
{
}
down voteacceptedTo count files and directories non-recursively you can use os.listdir and take its length.
To count files and directories recursively you can use os.walk to iterate over the files and subdirectories in the directory.
If you only want to count files not directories you can use os.listdir and os.path.file to check if each entry is a file:
import os for filename in os.listdir(os.getcwd()): # do your stuff
location = os.getcwd() # get present working directory location here counter = 0 pyfiles = [] #list to store all py files found at location otherfiles = [] #list to keep any other file that do not match the criteria for file in os.listdir(location): try: if file.endswith(".py"): print "python file found: ", file pyfiles.append(str(file)) counter = counter+1 else: otherfiles.append(file) except Exception as e: raise e print "No files found here!" print "Total python files found: ", counter This will print out the lines of each file in the pyfiles[]. You can also pass any mode.
Countline(path)
{
import filemapper as fm all_files = fm.load('pyfiles') for f in all_files: for i in fm.read(f):print i }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.