1 / 14

14.3 Files and Streams

0. 1. 2. 3. 4. 5. 6. 7. 8. 9. n-1. end-of-file marker. 14.3 Files and Streams. Python views files as sequential streams of bytes Each file ends with an end-of-file marker Opening a file creates an object associated with a stream. Fig. 14.2 Python’s view of a file of n bytes.

sana
Télécharger la présentation

14.3 Files and Streams

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. 0 1 2 3 4 5 6 7 8 9 ... n-1 ... end-of-file marker 14.3 Files and Streams • Python views files as sequential streams of bytes • Each file ends with an end-of-file marker • Opening a file creates an object associated with a stream Fig. 14.2 Python’s view of a file of n bytes.

  2. 14.3 Files and Streams • Three file streams created when Python program executes – sys.stdin (standard input stream), sys.stdout (standard output stream) and sys.stderr (standard error stream) • “Communication channels” to devices • Defaulted to keyboard, screen and screen, respectively (raw_input uses stdin, print uses stdout) • Redirect: print >> file, “football is fun”

  3. Open “clients.dat” in write mode EOFError generated when user enters EOF character 1 # Fig. 14.3: fig14_03.py 2 # Opening and writing to a file. 3 4 import sys 5 6 # open file 7 try: 8 file = open( "clients.dat", "w" ) # open file in write mode 9except IOError, message: # file open failed 10print >> sys.stderr, "File could not be opened:", message 11 sys.exit( 1 ) 12 13 print"Enter the account, name and balance." 14 print"Enter end-of-file to end input." 15 16 while 1: 17 18 try: 19 accountLine = raw_input( "? " ) # get account entry 20except EOFError: 21 break# user entered EOF 22 else: 23print >> file, accountLine # write entry to file 24 25file.close() Redirect error message to sys.stderr (common practice, only effect if stderr is redirected) fig14_03.py Terminating program with argument 1 indicates error Write user input to file – print output redirected to file Close file Enter the account, name and balance. Enter end-of-file to end input. ? 100 Jones 24.98 ? 200 Doe 345.67 ? 300 White 0.00 ? 400 Stone -42.16 ? 500 Rich 224.62 ? ^Z In fact, ctrl-d is end-of-file

  4. 14.4 Creating a Sequential-Access File

  5. 14.4 Creating a Sequential-Access File

  6. 14.4 Creating a Sequential-Access File

  7. Reading Data from a Sequential-Access File Create list of file lines Format information in each line for output 1 # Fig. 14.6: fig14_06.py 2 # Reading and printing a file. 3 4 import sys 5 6 # open file 7 try: 8 file = open( "clients.dat", "r" ) 9 except IOError: 10 print >> sys.stderr, "File could not be opened" 11 sys.exit( 1 ) 12 13records = file.readlines() # retrieve list of lines in file 14 15 print"Account".ljust( 10 ), 16 print"Name".ljust( 10 ), 17 print"Balance".rjust( 10 ) 18 19for record in records: # format each line 20 fields = record.split() 21 print fields[ 0 ].ljust( 10 ), 22 print fields[ 1 ].ljust( 10 ), 23 print fields[ 2 ].rjust( 10 ) 24 25 file.close() fig14_06.py More on this next time More on this next time, acts like a string tokenizer: splits the string “100 Jones 24.09” into strings “100”, “Jones” and “24.09” Account Name Balance 100 Jones 24.98 200 Doe 345.67 300 White 0.00 400 Stone -42.16 500 Rich 224.62

  8. Intermezzo 2 www.daimi.au.dk/~chili/CSS/Intermezzi/24.9.2.html • Copy the file /users/chili/CSS.E03/Intermezzi/datafile.txt to your own directory. • Write a program that reads this file and prints out every second line (you might look at Figure 14.6, page 470). • Modify your program so that it writes every second line of the input file to a new file called scrambled_data.txt (see Figure 14.3, page 466).

  9. Solution try: fileIn = open("datafile.txt", "r") fileOut = open("scrambled_data.txt", "w") except IOError: print >> sys.stderr, "File could not be opened" sys.exit(1) lines = fileIn.readlines() shouldprint = 1 for line in lines: if shouldprint: print >> fileOut, line, shouldprint = 1 - shouldprint fileIn.close() fileOut.close()

  10. Representing a Biological Sequence

  11. A program that uses this format

  12. Program Interaction Control-d to end (end-of-file) Type of sequence? dna Name of sequence? cow ID of sequence? 1 The sequence itself: acgaagtc Type of sequence? dna Name of sequence? goat ID of sequence? 2a The sequence itself: cgagaagcactaa Type of sequence? dna Name of sequence? 2b ID of sequence? sheep The sequence itself: agacaaggatta Type of sequence? threonine:~%

  13. seqdic.data Name: cow ID: 1 Type: dna Length: 8 acgaagtc Name: 2b ID: sheep Type: dna Length: 12 agacaaggatta Name: goat ID: 2a Type: dna Length: 13 cgagaagcactaa

More Related