Question
Using the following guidelines, create a python program. 1. You will be using the CPT180Stuff folder from Chapter 9 Assignment 1 for this assignment. 3.
Using the following guidelines, create a python program.
1. You will be using the CPT180Stuff folder from Chapter 9 Assignment 1 for this assignment.
3. Add four comment lines at the top of the program that contain:
a. Program Name
b. Program Description
c. Programmer's Name (You)
d. Current Date
4. Write code that will do the following:
a. Using an if statement, determine if both the dognames.txt file and the catnames.txt file exist. If both files exist, the program should complete steps b - m below. If either file does not exist, the program should print out the message "Unable to access one or more files".
b. Open the file named dognames.txt in the CPT180Stuff\pets\dogs folder in the read mode.
c. Print the contents of the file.
d. Close the file.
e. Open the file named catnames.txt in the CPT180Stuff\pets\cats folder in the read mode.
f. Print the contents of the file.
g. Close the file.
h. Open the catnames.txt file in the append mode.
i. Add two cat names to the file. You can pick the names. The example output below shows the names Mortimer and Tigger added.
j. Close the file.
k. Open the catnames.txt file in read mode.
l. Print the contents of the file.
m. Close the file.
Below is my code: I am unsure what I am doing wrong in trying to get the new names to append to the file:
import os.path
cats = "C:\cpt180Stuff\pets\cats\catnames.txt" dogs = "C:\cpt180Stuff\pets\dogs\dognames.txt" if (os.path.exists(dogs)) and (os.path.exists(cats)) :
dogs = open(dogs, 'r') print(dogs.read()) dogs.close() cats = open(cats, 'r') print(cats.read()) cats.close() cats = open(cats, 'a') cats.write('Mortimer ') cats.write('Tigger ') cat.close() cats = open(cats, 'r') print(cats.read()) cats.close() else: print('Unable to access one of more files')
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started