Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

Evaluate: (a) P72 (b) P95 (c) P52 (d) P174

Answered: 1 week ago

Question

Understand the different approaches to job design. page 184

Answered: 1 week ago