Question: #program name: workWithFiles2.py #program description: here we are checking whether two files exist and if they exists print out the contents in it and perform

#program name: workWithFiles2.py

#program description: here we are checking whether two files exist and if they exists print out the contents in it and perform

#name :bhumika patel

if(path.exists('cpt180Stuff2/pets/dogs/dognames.txt') and path.exists('cpt180Stuff2/pets/cats/catnames.txt')):

f1= open('pets/dogs/dognames.txt','r')

print(f1.read())

f1.close()

print(" ")

f2= open('pets/cats/catnames.txt','r')

print(f2.read())

f2.close()

print(' ')

f2= open('pets/cats/catnames.txt','a')

f2.write(' ')

f2.write('Mortimer')

f2.write(' ')

f2.write('Tigger')

f2.close()

f2= open('pets/cats/catnames.txt','r')

print(f2.read())

f2.close()

else:

print("Unable to access one or more files")

This is my code and I am getting error saying as shown below. How can I correct that error ?

NameError: name 'path' is not defined

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!