-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise_16.py
56 lines (46 loc) · 1.36 KB
/
Exercise_16.py
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
#Exercise 16: Reading and Writing Files
from sys import argv
#Deletes a selected file from the current directory
script, filename = argv
print "We're going to erase %r" % filename
print "If you don't want to delete %r, hit CTRL-C (^C)."
print "If you do want that, type: delete"
print "If you want to view the contents of the file type: view"
def delete(filen):
print "Deleting file. . ."
target = open(filen, 'w')
target.truncate()
print "File Deleted Sucessfully!"
print "Closing file. . ."
target.close()
def write(filen):
print "Now I'm going to ask you for three lines."
target = open(filen, 'w')
line1 = raw_input("Line 1: ")
line2 = raw_input("Line 2: ")
line3 = raw_input("Line 3: ")
print "Writing lines to file. . ."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
print "Complete!"
target.close()
read = raw_input(">")
if(read == "view"):
file = open(filename)
print file.read()
read2 = raw_input("Delete? (yes/no)")
if(read2 == "yes"):
delete(filename)
else:
print "File Saved"
if(read == "delete"):
delete(filename)
read3 = raw_input("Would you like to add data to the file? (yes/no)")
if(read3 == "yes"):
write(filename)
else:
print "Complete. . ."