40 likes | 48 Views
This tutorial explains how to read and write files in Python, covering text input/output (I/O), binary I/O, and raw I/O. It discusses file opening, reading and writing, and provides examples and code snippets. It also highlights the differences between text, binary, and raw I/O.
E N D
http://xkcd.com/1586/ Input andOutput with FILES f = open('AliceInWonderland.txt', 'r') s = f.read() f.close() # The contents of the ENTIRE FILE # are now in the single STRING s f = open('my_new_file.txt', 'w') f.write('This is\n') f.write('how you') f.write('write to a file.\n') f.close() # Careful! This OVERWRITES the# file if it already exists. This video shows how to do FILE inputand output, but WITHOUT handling EXCEPTIONS. A subsequent video shows how to handle EXCEPTIONS, in particular, exceptions relevant to reading and writing with files.
Input andOutput with FILES Text input/output (I/O) expects and produces strings. In text I/O, the system takes care of any translations from the 0s and 1s that form the bits (that is, 0s and 1s) of the file to the characters from which strings are formed. Files that Notepad can read are text files. This video focuses on text I/O. BinaryI/O, also called buffered I/O, expects and produces bytes objects. A bytes object is an immutable sequence of integers in the range 0 to 256. As such, each item in a bytes sequence can be stored in 8 bits, since 8 bits yield 28, or 256, distinct sequences of bits. JPEG files (for storing images) require binary I/O. Raw I/O, also called unbuffered I/O, is input/output closer to the hardware. Doing this form of I/O leaves more to the user, but also provides more direct access to the hardware, as is needed by some kinds of programs.
Input and Output: Getting input from a file is called READING the file. Putting data into a file is called WRITING to the file. You must OPEN a file before reading/writing it. Opening a file lets the operating system interact with the file system on your program’s behalf, including arranging for efficient exchange of data. Input andOutput withFILES f = open('AliceInWonderland.txt', 'r') f = open('my_new_file.txt', 'w') s = f.read() f.close() The builtinopen function returns a file object, aka stream, aka file-like object. All subsequent operations are performed on the file object. f.write('This is\n') f.write('how you') f.write('write to a file.\n') f.close()
Input andOutput with FILES: Summary BinaryI/O, also called buffered I/O, expects and produces bytes objects. Text input/output (I/O) expects and produces strings. In text I/O, the system takes care of any translations from the 0s and 1s that form the bits (that is, 0s and 1s) of the file to the characters from which strings are formed. Files that Notepad can read are text files. Raw I/O, also called unbuffered I/O, is closer to the hardware. defreading_in_one_chunk(): f = open('AliceInWonderland.txt', 'r') s = f.read() f.close() ... s.count('!') ... ... s[0]) ... fork in range(10): print(s[k]) defwriting(): f = open('my_new_file.txt', 'w') f.write('This is\n') f.write('how you') f.write('write to a file.\n') f.close() The string s now contains the ENTIRE file. So you are now working with a STRING (and no longer with a FILE). defreading_line_by_line(): f = open('../foo/MoreAlice.txt', 'r') for line in f: if'garden'in line: print('Line with garden:', line) f.close() The string line takes on the value of each line in the file, line by line.