-
Notifications
You must be signed in to change notification settings - Fork 40
/
read_file.py
33 lines (26 loc) · 1.32 KB
/
read_file.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
#!/usr/bin/env python
#-----------------------------------------------------------------------------
# This example will open a file and read it line by line, process each line,
# and write the processed line to standard out.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Place any necessary functions below this.
#-----------------------------------------------------------------------------
def process_line(l):
# Place any line processing code here.
return l
#-----------------------------------------------------------------------------
# Begin the main program.
#-----------------------------------------------------------------------------
# Open a file for reading. Use 'rb' instead of 'r' if the file is binary.
infile = open('infilename.txt', 'r')
# Processing text files by line is typical but processing binary files by line
# is not. When processing binary files use .read(), .seek(), and .tell()
for line in infile:
# Strip any line endings from the line(\r, \n, or \r\n) then process the
# line using the process_line function
data = process_line(line.rstrip('\r\n'))
# Write the processed line to standard out.
print data
# Close the file because we are done with it.
infile.close()