140 likes | 340 Views
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.
E N D
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.
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”
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
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
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).
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()
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:~%
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