Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am stuck on this problem. I am having trouble figuring out how to call the 'def viewFile' to the menu option 7. Here is

I am stuck on this problem. I am having trouble figuring out how to call the 'def viewFile' to the menu option 7. Here is the problem:

Add a command to this chapter's case study program that allows the user to view the contents of a file in the current working directory. When the command is selected, the program should display a list of filenames and a prompt for the name of the file to be viewed. Be sure to include error recovery.

An example of the program input and output is shown below:

/root/sandbox 1 List the current directory 2 Move up 3 Move down 4 Number of files in the directory 5 Size of the directory in bytes 6 Search for a file name 7 View the contents of a file 8 Quit the program Enter a number: 7 Files in /root/sandbox: filesys.py example.txt Enter a file name from these names: example.txt THIS IS CONTENT OF AN EXAMPLE FILE. /root/sandbox 1 List the current directory 2 Move up 3 Move down 4 Number of files in the directory 5 Size of the directory in bytes 6 Search for a file name 7 View the contents of a file 8 Quit the program Enter a number: 8 Have a nice day! 

And here is my code:

importos,os.path

QUIT='8'

COMMANDS=('1','2','3','4','5','6','7','8')

MENU="""1Listthecurrentdirectory

2Moveup

3Movedown

4Numberoffilesinthedirectory

5Sizeofthedirectoryinbytes

6Searchforafilename

7Viewthecontentsofafile

8Quittheprogram"""

defmain():

whileTrue:

print(os.getcwd())

print(MENU)

command=acceptCommand()

runCommand(command)

ifcommand==QUIT:

print("Haveaniceday!")

break

defacceptCommand():

"""Inputsandreturnsalegitimatecommandnumber."""

whileTrue:

command=input("Enteranumber:")

ifnotcommandinCOMMANDS:

print("Error:commandnotrecognized")

else:

returncommand

defrunCommand(command):

"""Selectsandrunsacommand."""

ifcommand=='1':

listCurrentDir(os.getcwd())

elifcommand=='2':

moveUp()

elifcommand=='3':

moveDown(os.getcwd())

elifcommand=='4':

print("Thetotalnumberoffilesis",\

countFiles(os.getcwd()))

elifcommand=='5':

print("Thetotalnumberofbytesis",\

countBytes(os.getcwd()))

elifcommand=='6':

target=raw_input("Enterthesearchstring:")

fileList=findFiles(target,os.getcwd())

ifnotfileList:

print("Stringnotfound")

else:

forfinfileList:

print(f)

elifcommand=='7':

print("Filesin",os.getcwd())

print(listCurrentDir(os.getcwd()))

viewFile=input("Enterafilenamefromthesenames:")

viewFile()

defviewFile(dirName):

f=open('viewFile','r')

print(f.read)

deflistCurrentDir(dirName):

"""Printsalistofthecwd'scontents."""

lyst=os.listdir(dirName)

forelementinlyst:print(element)

defmoveUp():

"""Movesuptotheparentdirectory."""

os.chdir("..")

defmoveDown(currentDir):

"""Movesdowntothenamedsubdirectoryifitexists."""

newDir=input("Enterthedirectoryname:")

ifos.path.exists(currentDir+os.sep+newDir)and\

os.path.isdir(newDir):

os.chdir(newDir)

else:

print("ERROR:nosuchname")

defcountFiles(path):

"""Returnsthenumberoffilesinthecwdand

allitssubdirectories."""

count=0

lyst=os.listdir(path)

forelementinlyst:

ifos.path.isfile(element):

count+=1

else:

os.chdir(element)

count+=countFiles(os.getcwd())

os.chdir("..")

returncount

defcountBytes(path):

"""Returnsthenumberofbytesinthecwdand

allitssubdirectories."""

count=0

lyst=os.listdir(path)

forelementinlyst:

ifos.path.isfile(element):

count+=os.path.getsize(element)

else:

os.chdir(element)

count+=countBytes(os.getcwd())

os.chdir("..")

returncount

deffindFiles(target,path):

"""Returnsalistofthefilenamesthatcontain

thetargetstringinthecwdandallitssubdirectories."""

files=[]

lyst=os.listdir(path)

forelementinlyst:

ifos.path.isfile(element):

iftargetinelement:

files.append(path+os.sep+element)

else:

os.chdir(element)

files.extend(findFiles(target,os.getcwd()))

os.chdir("..")

returnfiles

if__name__=="__main__":

main()

Please help!

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

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions

Question

What are the factors that influence make or buy decisions ?

Answered: 1 week ago