File I/O Dr. Nancy Warter-Perez
Introduction to Python – Part II2 Homework Submission Guidelines Submit your programs via Subject template for HWx: Chem434 HWx or Binf400 HWx Example for HW1P: Chem434 HW1P or Binf400 HW1P sample output (screen shot(s)) to show that your program is working properly
Introduction to Python – Part II3 Homework Due Dates Programming Homework Dues Dates are somewhat flexible because of the diverse programming skills of students in the class. Always submit your homework on the due date. If the program is not working, also submit an explanation of where the problem is. If the program is working, submit screen shots that demonstrate your program is working properly. You have up to one week to resubmit your homework (with no penalty) after the due date. Follow the same guidelines (i.e., if working submit screen shots and if not working, submit explanation (and screen shots if possible).
Introduction to Python – Part II4 File Inputs and Outputs (I/O) Data can be read directly from a file and results can be written directly to a file. First you need to open the file: Output Ex: outfile = open("out.txt", 'w') 'w' indicates that the file will be written to by the script (i.e., it is an output file) The file out.txt will be created by the script in the same directory as your script The variable outfile will allow you to refer to the out.txt file in your program. You can use any variable name you want.
Introduction to Python – Part II5 File Inputs and Outputs (I/O) Another example of opening a file: Input Ex: infile = open("D:\\Docs\\test.txt", 'r') 'r' indicates that the file will be read by the script (i.e., it is and input file) The file test.txt should already exist in the folder specified Notice that here the exact path of where to find the file is given (" D:\\Docs\\test.txt") The variable infile will allow you to refer to test.txt file in your program. You can use any variable name that you want.
Introduction to Python – Part II6 File Inputs and Outputs (I/O) Second, after opening the file you can either read or write to it. Input Ex: s = infile.read() The entire contents of the input file (test.txt) will be read in as a string and stored in s. Refer to slide 9 for more input file operations. Output Ex: outfile.write(str) This will write the contents of the string variable (str) into the file (out.txt) Refer to slide 10 for more output file operations.
Introduction to Python – Part II7 File Inputs and Outputs (I/O) Third, after you are done with the file, close it so that other applications can access the file. Input Ex: infile.close() Output Ex: outfile.close()
Introduction to Python – Part II8 Common input file operations OperationInterpretation input = open ('file', 'r')open input file S = input.read()read entire file into string S S = input.read(N)Read N bytes (N>= 1) S = input.readline()Read next line L = input.readlines()Read entire file into list of line strings
Introduction to Python – Part II9 Common output file operations OperationInterpretation output = open('file', 'w')create output file output.write(S)Write string S into file output.writelines(L)Write all line strings in list L into file output.close()Manual close (good habit)
Introduction to Python – Part II10 Example Write a script to read in a string of integer numbers from a file, convert each character (‘0’ to ‘9’) to an integer, and store it into an output file
Introduction to Python – Part II11 Example Python Script # Example file I/O script # Script will read in an input string of numbers from an input file, # convert each character to an integer and write the converted # values to an output file. # # Written by: Prof. Nancy Warter-Perez # Date created: May 5, 2009 infile = open("input.txt", "r") # before running the script, create input.txt with a string of characters outfile = open("output.txt", "w") # will be created by the program. s = infile.read() #read in entire file into s (note, there are other options for reading files) infile.close() #done reading the file for c in s: outfile.write("%s," % int(c)) #writes each value to the output file separated by comma outfile.close()
Introduction to Python – Part II12 Example input and output files Contents of input.txt before (and after) running the script Contents of output.csv after running the script 1,2,3,4,5,6,7,8,9,8,7,6,5,4,5,6,7,8,9,
Introduction to Python – Part II13 Plot using Excel In Excel, open the output.txt file (as a text file) Choose delimited data type and select comma as a delimiter Select the row of data Select the output graph format Example: