1 / 4

File Input/Output: Reading and Writing with Python

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.

tammyr
Download Presentation

File Input/Output: Reading and Writing with Python

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. 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.

  2. 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.

  3. 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()

  4. 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.

More Related