Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python The first line of the input is the path to the directory in which the sea

ID: 671581 • Letter: P

Question

Python

The first line of the input is the path to the directory in which the search for files should be rooted. For example, a Windows user might type C:Program Files or D:Python34Lib, in which case only files in or "underneath" the chosen directory will be found.
If the first line of the input is not a valid path to a directory (e.g., not a path to a directory that exists, or is not a valid path at all), print the word ERROR on a line by itself and repeat reading a line of input; continue until the input is a valid path to a directory.
The second line of the input specifies the search characteristics that will be used in deciding whether files are "interesting" and should have action taken on them. There are three different search characteristics; the second line of the input chooses one of them.
If the second line of the input begins with the letter N, the search will be for files whose names exactly match a particular name. The N will be followed by space; after the space, the rest of the line will indicate the name of the files to be searched for.
If the second line of the input begins with the letter E, the search will be for files whose name end in a particular extension. The E will be followed by a space; after the space, the rest of the line will indicate the desired extension.
For example, if the desired extension is py, all files whose names end in .py will be considered interesting. The desired extension may be specified with or without a dot preceding it (e.g., E .py or E py would mean the same thing in the input), and your search should behave the same either way.
Note, also, that there is a difference between what you might call a name ending and an extension. In our program, if the search is looking for files with the extension oc, a file named iliveinthe.oc would be found, but a file named invoice.doc would not.
If the second line of the input begins with the letter S, the search will be for files whose size, measured in bytes, strictly exceeds (i.e., is greater than) a specified threshold. The S will be followed by a space; after the space, the rest of the line will be a non-negative integer value specifying the size threshold.
For example, the input S 2097151 means that files whose sizes are at least 2,097,152 bytes (i.e., greater than 2,097,151 bytes) will be considered interesting.
If the second line of the input does not match one of the formats described above, print the word ERROR on a line by itself and repeat reading a line of input; continue until the input is a valid path to a directory.

Explanation / Answer

import os
searchDir = input('Enter the path to the directory in which the search for files should be rooted')


flag = 1;
while(flag):

     if (os.path.isdir(searchDir)):
           flag = 0
     else:
            searchDir = input('ERROR ! Enter valid path')

else:
    option = input()
    print ("Option: ",option)
    fields = option.split(" ")
    if(fields[0] == 'N'):
        file = searchDir+"/"+fields[1]
        if(os.path.isfile(file)) :
            print ("File exist ")
        else:
            print ("File not exist in the directory")
    elif (fields[0] == 'E'):
          
            if ('.' in fields[1]):
                fileExt = fields[1]
            else :
               fileExt = '.'+fields[1]
            print ("fileExt : ",fileExt)
            for root, dirs, files in os.walk(searchDir):
                for f in files:
                    fullpath = f #os.path.join(root, f)
                    if os.path.splitext(fullpath)[1] == fileExt:
                        print (fullpath)
              
    elif (fields[0] == 'S'):
         for root, dirs, files in os.walk(searchDir):
                filesize = int(fields[1])
                for f in files:
                    fullpath = os.path.join(root, f)
                    if os.path.getsize(fullpath) > filesize:
                        print (fullpath)
    else:
        print ("Invalid option")

/////ouptu////////////////////////////////////

C:UsersRavi>python D: aviChegpython-filedirectory.txt
Enter the path to the directory in which the search for files should be rooted
ERROR !
Enter valid pathD: aviCheg
E txt
Option: E txt
fileExt : .txt
2nNumbersAspairs.txt
2scomplement.txt
bubblesort.txt
C-dateExercise.txt
C-distance.txt
childFirst.txt
DAtabaseQueriesSSN.txt
DinningPhilsopher.txt
fork.txt
fortran-bacteriaGrowth.txt
hmmmCode.txt
inclusion-exclusion.txt
logicalExpression.txt
matchingNameSQLquery.txt
matlab-apointliesonCenter.txt
Matlab-arctan.txt
Matlab-arrayoper.txt
Matlab-cardShuffling.txt
matlab-cost.txt
matlab-exp(2)-maclaurinseriex.txt
Matlab-fixedpointiteration.txt
Matlab-futurevalue.txt
Matlab-goalpost.txt
Matlab-nestedstructures.txt
Matlab-NumberDivs-facFun.txt
matlab-Snfunction.txt
Matlab-transposematrix.txt
Matlab.txt
MAtlabVectorNorm.txt
MIPS-Collatz conjecture.txt
MIPshexAdditinEtc.txt
OSPG.txt
pipes-c.txt
PseudoCode4MinMax.txt
python-filedirectory.txt
python-linkedlist.txt
RelationalQueryLanguages.txt
significatBytesEtcInC.txt
twostacks.txt
VB-charFreuency.txt

C:UsersRavi>