Skip to content

Commit dafe8ae

Browse files
committed
file input-output read-write operations
1 parent 7a4e99d commit dafe8ae

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

file_ops.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
infile = "inputFile.txt"
2+
outfile = "outputFile.txt"
3+
4+
# print each line, as read in
5+
with open(infile) as f1:
6+
for line in f1:
7+
print (line)
8+
9+
print ("\n*******************")
10+
11+
# print each line, stripping last newline character
12+
with open(infile) as f1:
13+
for line in f1:
14+
print (line[:-1])
15+
16+
print ("\n*******************")
17+
18+
# print makes only (first word of each line)
19+
with open(infile) as f1:
20+
for line in f1:
21+
row = line.split(",")
22+
print(row[0])
23+
24+
print ("\n*******************")
25+
26+
# print each line as a formatted list
27+
with open(infile) as f1:
28+
for line in f1:
29+
row = line.split(",")
30+
print(row[0] + "\n-----------------")
31+
for i in range(1, len(row)):
32+
print(row[i])
33+
34+
print ("\n*******************")
35+
36+
# add each line to a list
37+
cars = list()
38+
with open(infile) as f1:
39+
for line in f1:
40+
row = line.split(",")
41+
cars.append(row)
42+
print(cars[0][0])
43+
44+
# write Makes only to outputFile
45+
with open(outfile, 'a') as f2:
46+
for car in cars:
47+
f2.write(car[0] + "\n")
48+
49+
# write list of row-lists to outputFile
50+
with open(outfile, 'a') as f2:
51+
for car in cars:
52+
f2.write(str(car) + "\n")
53+

0 commit comments

Comments
 (0)