The input Your program will take input from the console in the following format.
ID: 3662572 • Letter: T
Question
The input
Your program will take input from the console in the following format. It should not prompt the user in any way; it should simply read whatever input is typed into the console, and you should assume that your user knows the precise input format. A description of that format follows.
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 typeC: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 wordERROR 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 valid.
The third line of the input specifies the action that should be taken on each of the interesting files found in the search. No matter what, you should always print the file's path, on its own line of output, to the console when you find an interesting one; the action chosen here specifies what else should be done with it.
If the third line of the input contains the letter P by itself, print the file's path to the console — just as you will always do — but otherwise don't do anything with it.
If the third line of the input contains the letter F by itself, open the file under the assumption that the file is a text read, read the first line of text from the file, and print that text to the console. (If the file does not contain text, it's fine if this choice prints unreadable garbage or even crashes your program; testing that a file is a text file is trickier than it sounds, so we'll only test this feature on text files.)
If the third line of the input contains the letter D by itself, make a duplicate copy of the file and store it in the same directory where the original resides, but the copy should have .dup (short for "duplicate") appended to the filename. For example, if the interesting file isC:picturesoo.jpg, you would copy it to C:picturesoo.jpg.dup.
If the third line of the input contains the letter T by itself, "touch" the file, which means to modify its last modified timestamp to be the current date/time.
If the third 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 valid.
I do have most of the code but my third line of input is not working? Can someone help me finish or edit my code? Thanks in advance.
import os.path
import os
import shutil
name = input()
def search_files():
p = os.path.join(name)
statement = os.path.exists(p)
if statement == False:
print('Error')
print(search_files())
else:
return p
print(search_files())
def search_characteristics():
interesting = input()
space_count = interesting.count(' ')
new_list = []
new_list1 = []
p = os.path.join(name)
files = nested_dir(p)
statement = os.path.exists(p)
keyword = interesting[space_count+1:len(interesting)+1]
if interesting[0] == 'N':
return os.path.join(p,interesting[space_count:len(interesting)+1])
elif interesting[0] == 'E':
for item in files:
if item[item.rfind('.')+1:len(item)+1] == interesting[space_count:len(interesting)+1]:
new_list.append(item)
return new_list
elif interesting[0] == 'S':
for item in files:
if int(keyword) < os.path.getsize(item):
new_list1.append(item)
return new_list1
else:
print('Error')
print(search_characteristics())
def nested_dir(nested_dire) -> list:
file_list = []
for element in os.listdir(nested_dire):
path = os.path.join(nested_dire, element)
print(path)
if os.path.isdir (path):
file_list.extend(nested_dir(path))
else:
file_list.append(path)
return file_list
print(search_characteristics())
def print_path(intersting_file):
for file in interesting_file:
print(file)
def read_file(interesting_file):
for file in interesting_file:
infile = Path(file)
openfile=infile.open()
print(openfile.readline().rstrip())
openfile.close()
def duplicate(interesting_file):
for file in interesting_file:
shutil.copy(file,file+'.dup')
if __name__ =='_main_':
while True:
file_input = Path(input()).absolute()
UserInterface()
if file_input.lower() == "P":
print_path(Found_file)
break
elif file_input.lower() == "F":
read_file()
break
elif file_input.lower() == "D":
duplicate()
break
elif file_input.lower() == "T":
os.utime()
break
else:
print("Error")
Explanation / Answer
Modified Python File:
import os.path
import os
import shutil
import pdb
#Method for searching files
def search_files(path_ip):
p = os.path.join(path_ip)
statement = os.path.exists(p)
if statement == False:
print('Error');
path_ip = input();
search_files();
else:
print('Correct');
#Method for searching characteristics
def search_characteristics(path_ip):
interesting = input()
space_count = interesting.count(' ')
new_list = []
new_list1 = []
new_list2 = []
p = os.path.join(path_ip)
files = nested_dir(p)
statement = os.path.exists(p)
keyword = interesting[space_count+1:len(interesting)+1]
if interesting[0] == 'N':
for item in files:
if item[item.rfind('\')+1:item.rfind('.')] == interesting[space_count+1:len(interesting)+1]:
new_list2.append(item)
return new_list2
elif interesting[0] == 'E':
for item in files:
if item[item.rfind('.')+1:len(item)+1] == ((interesting[space_count:len(interesting)+1]).strip(' ')).strip('.'):
new_list.append(item)
return new_list
elif interesting[0] == 'S':
for item in files:
if int(keyword) < os.path.getsize(item):
new_list1.append(item)
return new_list1
else:
print('Error')
#Method for getting all the files in the directory
def nested_dir(nested_dire) -> list:
file_list = []
for element in os.listdir(nested_dire):
path = os.path.join(nested_dire, element)
if os.path.isdir (path):
file_list.extend(nested_dir(path))
else:
file_list.append(path)
return file_list
#Method for printing the path
def print_path(intersting_file):
for file in intersting_file:
print(file)
#Method for reading file
def read_file(interesting_file):
for file in interesting_file:
infile = file;
openfile=open(infile)
for line in openfile.readlines():
print(line);
openfile.close()
#Method for making duplicates
def duplicate(interesting_file):
for file in interesting_file:
shutil.copy(file,file+'.dup')
#Method for modifying file
def touch(interesting_file):
for file in interesting_file:
os.utime(file,(1330712280, 1330712292))
#Method for executing operations
def operations(op):
while True:
file_input = input()
if file_input.lower() == "p":
print_path(op)
break
elif file_input.lower() == "f":
read_file(op)
break
elif file_input.lower() == "d":
duplicate(op)
break
elif file_input.lower() == "t":
touch(op);
break
else:
print("Error")
path_ip = input();
search_files(path_ip);
op = search_characteristics(path_ip);
print(op);
operations(op);
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
C:Python33>python files.py
f:Java
Correct
N Account
['f:\Java\Account.java']
p
f:JavaAccount.java
C:Python33>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.