Files and Streams in Microsoft Office Implementation QR Code JIS X 0510 in Microsoft Office Files and Streams

14.3 Files and Streams using microsoft toget qrcode for web,windows application console app Python views each file as a QR Code 2d barcode for None sequential stream of bytes (Fig. 14.2).

Each file ends either with an end-of-file marker or at a specific byte number recorded in a system-maintained administrative data structure. When a program opens a file, Python creates an object and associates a stream with that object..



. end-of-file marker Fig. 14.2 Python s view of a file of n bytes. Page 466 Friday, December 14, 2001 2:06 PM File Processing and Serialization 14 . When a Python program begins QR-Code for None execution, Python creates three file streams sys.stdin (standard input stream), sys.stdout (standard output stream) and sys.

stderr (standard error stream). These streams provide communication channels between a program and a particular file or device. Python file streams are created regardless of whether a Python program imports the sys module, although a program must import the sys module to access the streams directly.

Program input corresponds to sys.stdin. In fact, raw_input uses sys.

stdin to retrieve user input. Program output corresponds to sys.stdout.

The print statement sends information to the standard output stream, by default. Program errors are printed to sys.stderr.

The sys.stdin stream enables a program to receive input from the keyboard or other devices, the sys.stdout stream enables a program to output data to the screen or other devices and the sys.

stderr stream enables a program to output error messages to the screen or other devices.. 14.4 Creating a Sequential-Access File Python imposes no structure Microsoft QR Code ISO/IEC18004 on a file notions like records do not exist in Python files. This means that the programmer must structure files to meet the requirements of applications. In the example in this section, we impose a record structure on a file.

Figure 14.3 creates a simple sequential-access file that might be used by an accountsreceivable system to track the money owed by a company s client. For each client, the program obtains an account number, the client s name and account balance (i.

e., the amount the client owes the company). The data obtained for each client constitutes a record for that client.

The account number represents the record key in this application; that is, the file will be created and maintained in account-number order. In our example, we assume a user enters the account information in account-number order. In a comprehensive accountsreceivable system, a sorting capability would be provided so the user could enter the records in any order the records would then be sorted before being written to the file.

As stated previously, a programmer creates file-stream objects to open files. Function open, which receives one required argument and two optional arguments, creates a stream object (line 8). The required argument for the new stream object is the file name; the two optional arguments are the file-open mode and the buffering mode.

. 1 2 3 4 5 6 7 8 9 10 11 12 1 3 14 # Fig. 14.3: fig14_03.

py # Opening and writing to a file. import sys # open file try: file = open( "clients.dat", "w" ) # open file in write mode except IOError, message: # file open failed print >> sys.

stderr, "File could not be opened:", message sys.exit( 1 ) print "Enter the account, name and balance." print "Enter end-of-file to end input.

" File-stream objects for opening and writing data to a file. (Part 1 of 2.).

Fig. 14.3 Page 467 Friday, December 14, 2001 2:06 PM 14 . File Processing and Serialization 15 16 17 18 19 20 21 22 23 2 Microsoft Denso QR Bar Code 4 25. while 1: try: accountLine = raw_input( " " ) except EOFError: break else: print >> file, accountLine file.close() the account, name and balance. end-of-file to end input.

Jones 24.98 Doe 345.67 White 0.

00 Stone -42.16 Rich 224.62.

# get account entry # user entered EOF # write entry to file Enter Enter 100 200 300 400 500 ^Z Fig. 14.3 File-stream objects for open Quick Response Code for None ing and writing data to a file. (Part 2 of 2.).

The file-open mode indicates whether a user can open the file for reading, writing or both. File-open mode "w" opens a file to output data to the file. Existing files opened with mode "w" are truncated all data in the file is deleted and re-created with the new data.

If the specified file does not yet exist, then a file is created. The newly created file is assigned the name provided in the file name argument (i.e.

, clients.dat). If the location of the file is not specified in the file name argument, Python attempts to create the file in the current directory.

If the file open-mode argument is not specified, the default value is "r", which opens a file for reading. Figure 14.4 lists various file-open modes.

The third argument to function open the buffering-mode argument is for advanced control of file input and output and usually is not specified. We do not assign a value to the bufferingmode argument in this example..

Copyright © . All rights reserved.