Split tsv tabseparatedvalues each line has the values

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: le try: infile = open(file1, ”r”) outfile = open(file2, ”w”) count = 0 for line in infile: count = count + 1 outfile.write(line) print(“number of lines ”, count) infile.close() outfile.close() except: print(“Error in code”) CMPUT174  ­ ­ References & Files 12 4 13 ­10 ­21 Errors in reading/wri9ng files • Files exist outside of your Python program, and so it is hard to control what happens to them • It is a good idea to use try/except when working with files • For example: open(”a_file", "r")
 Traceback (most recent call last):
 File "<string>", line 1, in <fragment>
 builtins.IOError: [Errno 2] No such file or directory: 'a_file'" 13 CMPUT174  ­ ­ References & Files Accessing Files • Always wrap the instrucWons with try/except • Always close the file when you are done • Always check for empty lines (or comment lines) in your program • Get used to some common text formats:   “CSV” (comma ­separated ­values): each line has the values separated by “,”: use line.split(“,”)   “TSV” (t...
View Full Document

Ask a homework question - tutors are online