-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson_3.py
75 lines (54 loc) · 1.77 KB
/
lesson_3.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
#---------------------------------------------------------------------------#
# DAY 3
#---------------------------------------------------------------------------#
# FILE HANDLING - READING FILES AND WRITING FILES
from sys import argv
script, filename = argv
print(f"We're going to delete {filename}.")
print("To cancel, hit CTRL-C (^C).")
print("To proceed, hit RETURN.")
input('?')
# prints an indication, then opens the file
# w means opens the file as a writable file
print("Opening {filename}...")
target = open(filename, 'w')
# truncate, means deleting the content of the file
print("Deleting the content of {}...".format(filename))
target.truncate()
# asking for input then writes it in the file
print("Gimme three lines.")
line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
# or a prettier approach
target.write(f"{line1}\n{line2}\n{line3}")
# then closes the file
print(f"Closing the {filename}...")
target.close()
#---------------------------------------------------------------------------#
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print(f"Will copy from {from_file} to {to_file}.")
# opens the file that is passed as an argument
in_file = open(from_file)
indata = in_file.read()
# prints if the file exists
print(f"Does the file exist? {exists(to_file)}")
print("Ready? Hit enter to continue, CTRL-C to abort.")
input()
# opens the file that will be written upon
# this process overwrites the file
out_file = open(to_file, 'w')
out_file.write(indata)
print("DONE!")
# closes both files
out_file.close()
in_file.close()
#---------------------------------------------------------------------------#