Question: Python - OS les Complete the program below by writing the functions find and copy . The behavior of this program is as follows: def
Python - OS les Complete the program below by writing the functions find and copy. The behavior of this program is as follows:
def find()- takes a directory name root and a simple lename (without slashes) target and searches in the subtree rooted at root for all les whose simple name is target. It prints out a list of the absolute pathnames of these les. If root is not a valid directory name, it should print a message and exit immediately.
def copy() - it creates a new directory newdir and copies each of the found les to this directory. The target name for the copied le should be the absolute path name with the leading slash (/) removed and all other slashes replaced by underscores( ). It is ok to raise an exception if newdir already exists or cannot be created for some reason.
You will need to consult documentation for the os package (including the os.path subpackage) and the shutil package. These can be found in the standard Python documentation.
#!/usr/bin/env python3 #
import sys import os import shutil
def find():
/* Write your code here */
def copy():
/* Write your code here */
def usage(): print ('usage: ./find.py [--copy dirname] root file')
def main(): if len(sys.argv) < 2: usage() if sys.argv[1] == '--copy': if len(sys.argv) < 5: usage() new_dir = sys.argv[2] root = sys.argv[3] filename = sys.argv[4] found = find(root,filename) for f in found: print(f) copy(found,new_dir) else: if len(sys.argv) < 3: usage() root = sys.argv[1] filename = sys.argv[2] found = find(root,filename) for f in found: print(f)
if __name__ == '__main__': main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
