-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex16.py
79 lines (58 loc) · 2.08 KB
/
ex16.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Importing the argument variable - argv from the sys module
from sys import argv
#Definition and initialization of two argument variables.
script, filename = argv
# Anouncing the action of file deletion.
print "We're going to erase %r." % filename
# Opening the file named filename, and returning the value to txt variable
print "Opening the file for reading..."
target = open(filename)
# Read the file content.
print "Here is the file content."
print target.read()
# Giving the user the posibility not to delete the file.
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit ENTER/RETURN."
# Expectiong the user decision.
raw_input("?")
# Close the file and opening it for writing..
print "Close the file and open it for writing."
target.close()
# Truncating the file filename..
print "Truncating the file while opening for write and read. Goodbye!"
target = open(filename, 'w')
# Initiating a second action.
print "Now I'm going to ask you for thee lines."
# Getting the lines.
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
# Anouncing the write to file filename of the entered lines.
print "I'm going to write these to the file."
# # Write line1.
# target.write(line1)
# # Adding new-line caracter at the end of line1.
# target.write("\n")
# # Write line2.
# target.write(line2)
# # Adding new-line caracter at the end of line2.
# target.write("\n")
# # Write line3.
# target.write(line3)
# # Adding new-line caracter at the end of line3.
# target.write("\n")
file_content = line1 + "\n" + line2 + "\n" + line3 + "\n"
# Write lines all together..
target.write(file_content)
# Close the file before reading.
print "Closing and opening for reading."
target.close()
# Opening the file named filename, and returning the value to txt variable
print "Opening the file for reading..."
target = open(filename)
# Read the new data entered.
print "Here is the new file content."
print target.read()
# Close the file.
print "And finaly we close it."
target.close()