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

file processing
SMART_READER_LITE
LIVE PREVIEW

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.


slide-1
SLIDE 1

Fu Fundamentals of Pr Programming (Py Python)

File Processing

Ali Taheri Sharif University of Technology

Spring 2019

slide-2
SLIDE 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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-3
SLIDE 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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-4
SLIDE 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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-5
SLIDE 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:

5

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

f = open('output.txt', 'w') f = open('C:/Python36/README.txt', 'r')

slide-6
SLIDE 6

Opening Modes

6

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

Mode Description

‘r’

Open a file for reading (default).

‘w’

Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.

‘a’

Open for appending at the end of the file without 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

slide-7
SLIDE 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 It can be automatically done using with statement

7

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

f = open('output.txt', 'w') # some operations here f.close() with open('data.txt', 'a+') as f: # some operations here # now the file has been closed

slide-8
SLIDE 8

Writing to a File

The write method is used to output data to a file

  • Slow if needed to enter a lot of data
  • 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

  • The print function adds a new line to the end of the string by default

8

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

with open('data.txt', 'w') as f: f.write("my first file\n") f.write("This file\n\n") f.write("contains four lines\n") print('Sample text', file=f)

slide-9
SLIDE 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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-10
SLIDE 10

Reading from a File

Reading Characters

  • The read method is used to read characters from a

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’

10

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

with open('input.txt', 'r') as f: s = f.read(10) # read 10 characters s = f.read() # read to the end of file S = f.readline() # read the next line

slide-11
SLIDE 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’
  • We can also iterate over lines using a for loop
  • Each line contain ending newline character ‘\n’

11

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

with open('input.txt', 'r') as f: lines = f.readlines() # returns all lines as a list for line in f: print(line, end='')

slide-12
SLIDE 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

Reading from a file

  • We can use load function to read the variable in a file

12

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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” with open(‘file.txt', ‘rb') as f: print(pickle.load(f)) # Reads the variable in “file.txt”