file processing
play

File Processing Ali Taheri Sharif University of Technology Spring - PowerPoint PPT Presentation

Fu Fundamentals of Pr Programming (Py Python) File Processing Ali Taheri Sharif University of Technology Spring 2019 Outline 1. Sources of Input 2. Files 3. Opening a File 4. Opening Modes 5. Closing a File 6. Writing to a File 7.


  1. Fu Fundamentals of Pr Programming (Py Python) File Processing Ali Taheri Sharif University of Technology Spring 2019

  2. Outline 1. Sources of Input 2. Files 3. Opening a File 4. Opening Modes 5. Closing a File 6. Writing to a File 7. Reading from a File 2 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  3. Sources of Input User Input ◦ Slow if needed to enter a lot of data ◦ Error prone: user may enter wrong values ◦ What if we want to run again? ◦ What if we want an input based on inputs of previous executions. Files ◦ Enter data once into a file, save it, and reuse it. ◦ Good for large amounts of data ◦ Programs can use files to communicate ◦ Need to be able to read from and write to files 3 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  4. Files In Python, a file operation takes place in the following order: 1. Open the file 2. Read or write (perform operation) 3. Close the file Files can be opened for different purposes ◦ Reading, writing, updating Open files must be closed ◦ To release the resources ◦ To flush the output 4 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  5. Opening a File To access a file, it must first be opened The open function is used to open a file ◦ <variable> = open (<path>, <mode>) ◦ <variable> is of type file (file is a data type!) ◦ <path> is the path and name of the file ◦ <mode> is the opening mode (read, write, update) Example: f = open( 'output.txt' , 'w' ) f = open( 'C:/Python36/README.txt' , 'r' ) 5 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  6. Opening Modes Mode Description Open a file for reading (default). ‘r’ Open a file for writing. Creates a new file if it does ‘w’ not exist or truncates the file if it exists. Open for appending at the end of the file without ‘a’ truncating it. Creates a new file if it does not exist. ‘b’ Open in binary mode. ‘+’ Open a file for updating (reading and writing) f = open( "test.txt" ) # equivalent to 'r' f = open( "test.txt" , 'r+' ) # read from begin, write at end f = open( "img.bmp" , 'wb' ) # write in binary mode 6 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  7. Closing a File When we are done with operations to the file, we need to properly close the file The close method is used to close the file f = open( 'output.txt' , 'w' ) # some operations here f.close() It can be automatically done using with statement with open( 'data.txt' , 'a+' ) as f: # some operations here # now the file has been closed 7 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  8. Writing to a File The write method is used to output data to a file with open( 'data.txt' , 'w' ) as f: f.write( "my first file\n" ) f.write( "This file\n\n" ) ◦ Slow if needed to enter a lot of data f.write( "contains four lines\n" ) ◦ Error prone: user may enter wrong values ◦ We must include the newline characters ourselves to distinguish different lines. This can be done using the print function as well print( 'Sample text' , file=f) ◦ The print function adds a new line to the end of the string by default 8 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  9. Reading from a File When a file is opened for reading, an input pointer is positioned at the beginning of the file After reading, the pointer is moved after the last read character Reading in the text mode returns data in string When the input pointer reaches the end of file, any further read attempts results in an empty string 9 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  10. Reading from a File Reading Characters ◦ The read method is used to read characters from a file with open( 'input.txt' , 'r' ) as f: s = f.read(10) # read 10 characters s = f.read() # read to the end of file Reading Lines ◦ The readline method is used to read a single line of text ◦ The lineReturns empty string when reaches end of file ◦ contains ending newline character ‘ \ n’ S = f.readline() # read the next line 10 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  11. Reading from a File Reading Lines ◦ The readlines method returns all the lines as a list ◦ All the lines contain ending newline character ‘ \ n’ with open( 'input.txt' , 'r' ) as f: lines = f.readlines() # returns all lines as a list ◦ We can also iterate over lines using a for loop ◦ Each line contain ending newline character ‘ \ n’ for line in f: print(line, end='') 11 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  12. Binary Files Writing to a file ◦ We can use pickle module to operations on binary files ◦ Using dump function, we can write a variable to a file import pickle var = [(1, 'x', [3,4]), {1,2,3}] with open( ‘file.txt ' , ‘ wb' ) as f: pickle.dump(var, f) # Writes “ var ” to “file.txt” Reading from a file ◦ We can use load function to read the variable in a file with open( ‘file.txt ' , ‘ rb' ) as f: print(pickle.load(f)) # Reads the variable in “file.txt” 12 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

Recommend


More recommend