-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw8i-o.py
More file actions
60 lines (48 loc) · 2.28 KB
/
hw8i-o.py
File metadata and controls
60 lines (48 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# input output homework pirple
#Not sure where they talked about checking if a file exists in the training, but I googled it and found "import os" and just used that.
import os
'''
Details:
Create a note-taking program. When a user starts it up, it should prompt them for a filename.
If they enter a file name that doesn't exist, it should prompt them to enter the text they want to write to the file. After they enter the text, it should save the file and exit.
If they enter a file name that already exists, it should ask the user if they want:
A) Read the file
B) Delete the file and start over
C) Append the file
If the user wants to read the file it should simply show the contents of the file on the screen. If the user wants to start over then the file should be deleted and another empty one made in its place. If a user elects to append the file, then they should be able to enter more text, and that text should be added to the existing text in the file.
Extra Credit:
Allow the user to select a 4th option:
D) Replace a single line
If the user wants to replace a single line in the file, they will then need to be prompted for 2 bits of information:
1) The line number they want to update.
2) The text that should replace that line.
'''
fileName = input("What do you want the filename to be?") + ".txt"
if os.path.isfile(fileName):
choice = input("Do you want to: \n A) Read the File \n B) Delete the file and start over \n C) Append the file \n ")
if choice == "A":
File = open(fileName, 'r')
lines = File.readlines()
print(lines)
File.close()
elif choice == "B":
os.remove(fileName)
File = open(fileName,"w")
File.close()
print("The old file has been deleted and a new file has been created.")
elif choice == "C":
File = open(fileName,"a")
Filez = [File]
fileAppend = input("What would you like to add to the file?")
Filez.append(fileAppend)
File.write(str(Filez))
print("Your file has been appended.")
else:
print("Please only choose either A, B, or C.")
else:
data = input("What do you want to input into file?")
File = open(fileName, "w")
File.write(data)
File.close()
print("Your file has been created!")
File.close()