-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
file_ops.py
53 lines (41 loc) · 1.06 KB
/
file_ops.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
infile = "inputFile.txt"
outfile = "outputFile.txt"
# print each line, as read in
with open(infile) as f1:
for line in f1:
print (line)
print ("\n*******************")
# print each line, stripping last newline character
with open(infile) as f1:
for line in f1:
print (line[:-1])
print ("\n*******************")
# print makes only (first word of each line)
with open(infile) as f1:
for line in f1:
row = line.split(",")
print(row[0])
print ("\n*******************")
# print each line as a formatted list
with open(infile) as f1:
for line in f1:
row = line.split(",")
print(row[0] + "\n-----------------")
for i in range(1, len(row)):
print(row[i])
print ("\n*******************")
# add each line to a list
cars = list()
with open(infile) as f1:
for line in f1:
row = line.split(",")
cars.append(row)
print(cars[0][0])
# write Makes only to outputFile
with open(outfile, 'a') as f2:
for car in cars:
f2.write(car[0] + "\n")
# write list of row-lists to outputFile
with open(outfile, 'a') as f2:
for car in cars:
f2.write(str(car) + "\n")