Processing Data from Files n So far: n Inputs : n from user n - - PowerPoint PPT Presentation

processing data from files
SMART_READER_LITE
LIVE PREVIEW

Processing Data from Files n So far: n Inputs : n from user n - - PowerPoint PPT Presentation

Processing Data from Files n So far: n Inputs : n from user n "hard-wired" into program n Outputs : n "printing" on the screen n In practice, usually: n Input from file n Output to file 1


slide-1
SLIDE 1

Processing Data from Files

n So far:

n Inputs:

n … from user n … "hard-wired" into program

n Outputs:

n … "printing" on the screen

n In practice, usually:

n Input from file n Output to file

1

slide-2
SLIDE 2

Python Programming, 2/e 2

File Processing

n The process of opening a file involves

associating a file on disk with an object in memory.

n We can manipulate the file by manipulating this

  • bject.

n Read from the file n Write to the file

slide-3
SLIDE 3

Python Programming, 2/e 3

File Processing

n When done with the file, it needs to be closed.

Closing the file causes any outstanding

  • perations and other bookkeeping for the file to

be completed.

n In some cases, not properly closing a file could

result in data loss.

slide-4
SLIDE 4

Python Programming, 2/e 4

File Processing

n Reading a file into a word processor

n File opened n Contents read into RAM n File closed n Changes to the file are made to the copy stored in

memory, not on the disk.

n Aside: who uses Dropbox?

è interesting issues with access control (easy to “shoot yourself in the foot”, when multiple users edit same file)

slide-5
SLIDE 5

Python Programming, 2/e 5

File Processing

n Saving a word processing file

n The original file on the disk is reopened in a mode

that will allow writing (this actually erases the old contents)

n File writing operations copy the version of the

document in memory to the disk

n The file is closed

slide-6
SLIDE 6

Python Programming, 2/e 6

File Processing

n Working with text files in Python

n Associate a disk file with a file object using the open

function <filevar> = open(<name>, <mode>)

n Name is a string with the actual file name on the

  • disk. The mode is either ‘r’ or ‘w’ depending on

whether we are reading or writing the file.

n MyInfile = open("numbers.dat", "r")

slide-7
SLIDE 7

Python Programming, 2/e 7

File Methods

n <file>.read() – returns the entire remaining contents

  • f the file as a single (possibly large, multi-line) string

n <file>.readline() – returns the next line of the file.

This is all text up to and including the next newline character

n <file>.readlines() – returns a list of the remaining

lines in the file. Each list item is a single line including the newline characters.

slide-8
SLIDE 8

Python Programming, 2/e 8

File Processing

n Another way to loop through the contents of a

file is to read it in with readlines and then loop through the resulting list:

n MyInfile = open(someFile, "r")

for line in MyInfile.readlines(): # Line processing here MyInfile.close()

slide-9
SLIDE 9

Python Programming, 2/e 9

File Processing

n Python treats the file itself as a sequence of lines

Very convenient!

n MyInfile = open(someFile, "r")

for line in MyInfile: # process the line here MyInfile.close()

n Bottom line:

n Processing a text file, line by line?? Use a for loop!

slide-10
SLIDE 10

Python Programming, 2/e 10

File Processing

n Opening a file for writing prepares the file to receive data n If you open an existing file for writing, you wipe out the

file’s old contents. If the named file does not exist, a new

  • ne is created.

n Outfile = open("mydata.out", "w") n print(<expressions>, file=Outfile)

n Alternative (and main option in Python 2):

n Outfile.write(…)

n This is very convenient (better than in Python 2!):

  • 1. Develop code with print(..) statements
  • 2. When all works, add "file = Outfile" (and all what goes with it) to

the program!

slide-11
SLIDE 11

Python Programming, 2/e 11

File Processing

slide-12
SLIDE 12

File Processing: Examples

12

slide-13
SLIDE 13

From Reading to Writing Files …

13

slide-14
SLIDE 14

File Processing:

Read an input file, write two output files

14

slide-15
SLIDE 15

… two files are generated:

15