1 / 13

File I/O

File I/O. Dr. Nancy Warter-Perez. Homework Submission Guidelines. Submit your programs via Email nwarter@calstatela.edu Subject template for HWx: Chem434 HWx or Binf400 HWx Example for HW1P: Chem434 HW1P or Binf400 HW1P

Download Presentation

File I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File I/O Dr. Nancy Warter-Perez

  2. Homework Submission Guidelines • Submit your programs via Email • nwarter@calstatela.edu • Subject template for HWx: Chem434 HWx or Binf400 HWx • Example for HW1P: Chem434 HW1P or Binf400 HW1P • Email sample output (screen shot(s)) to show that your program is working properly Introduction to Python – Part II

  3. 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 II

  4. 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 II

  5. 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 II

  6. 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 II

  7. 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 II

  8. Common input file operations Introduction to Python – Part II

  9. Common output file operations Introduction to Python – Part II

  10. 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 II

  11. 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 II

  12. Example input and output files • Contents of input.txt before (and after) running the script • 1234567898765456789 • 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 II

  13. 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: Introduction to Python – Part II

More Related