13.1 Opening files file_object = open('myfile', 'r') line = file_object.readline(): import os file_name = os.path.join("c:", "My Documents", "test", "myfile") file_object = open(file_name,'r') 13.2 Closing files file_object = open("myfile", 'r') line = file_object.readline() # . . . any further reading on the file_object . . . file_object.close() 13.2 Opening files in other modes file_object = open("myfile", 'w') file_object.write(“Hello, World\n”) file_object.close() 13.4 Read and write text or binary data file_object = open("myfile", 'r') count = 0 while file_object.readline() != "": count = count + 1 print(count) file_object.close() file_object = open("myfile", 'r') print(len(file_object.readlines())) file_object.close() file_object = open("myfile", 'r') count = 0 for line in file_object: count = count + 1 print(count) file_object.close() # Open a file for reading. input_file = open("myfile", newline="\n") inpu_filet = open("myfile.txt", 'r') lines = input_file.readlines() input_file.close() output = open("myfile2.txt", 'w') output.writelines(lines) output.close() # Open a file for reading in binary mode. input_file = open("myfile", 'rb') # Read the first four bytes as a header string. header = input_file.read(4) # Read the rest of the file as a single piece of data. data = input_file.read() input.close() 13.5 Screen input/output >>> x = input("enter file name to use: ") enter file name to use: myfile >>> x 'myfile' >>> x = int(input("enter your number: ")) enter your number: 39 >>> x 39 >>> import sys >>> print("Write to the standard output.") Write to the standard output. >>> sys.stdout.write("Write to the standard output.\n") Write to the standard output. 30 >>> s = sys.stdin.readline() An input line >>> s 'An input line\n' >>> import sys >>> f= open("outFile.txt",'w') >>> sys.stdout = f >>> sys.stdout.writelines(["A first line.\n","A seconde line.\n"]) >>> print("A line from the print statement") >>> 3+4 >>> sys.stdout = sys.__stdout__ >>> f.close() >>> 3+4 7 >>> import sys >>> f= open("outFile.txt",'w') >>> print("A first line.","A seconde line.", file=f) >>> 3+4 7 >>> f.close() >>> 3+4 7 13.6 struct module import struct record_format = 'hd4s' record_size = struct.calcsize(record_format) result_list = [] input = open("data", 'rb') while 1: # Read in a single record. record = input.read(record_size) # If the record is empty, it indicates we have reached the end of file, so quit the loop. # Note that we have made no provision for checking for file consistency, # i.e. that the file contains a number of bytes which is an integer multiple # of the record size. However, if the last record is an "odd" size, the # struct.unpack function will raise an error. if record == '': input.close() break # Unpack the record into a tuple, and append that tuple to the result list. result_list.append(struct.unpack(record_format, record)) >>> import struct >>> record_format = 'hd4s' >>> struct.pack(record_format, 7, 3.14, 'gbye') b'\x07\x00\x00\x00\x1f\x85\xebQ\xb8\x1e\t@gbye' 13.7 pickling >>> import pickle >>> file = open("solecache",'w') >>> pickle.dump({}, file) >>> file.close() 13.8 Shelving >>> import shelve >>> book = shelve.open("addresses") >>> book['flintstone'] = ('fred', '555-1234', '1233 Bedrock Place') >>> book['rubble'] = ('barney', '555-4321', '1235 Bedrock Place') >>> book..close() >>> import shelve >>> book = shelve.open("addresses") >>> book['flintstone'] ('fred', '555-1234', '1233 Bedrock Place')