Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input and Output with FILES

Similar presentations


Presentation on theme: "Input and Output with FILES"— Presentation transcript:

1 Input and Output with FILES
Input and Output 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 input and 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 and Output 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. Binary I/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 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 with FILES
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. f = open('AliceInWonderland.txt', 'r') f = open('my_new_file.txt', 'w') s = f.read() f.close() The builtin open 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 and Output with FILES: Summary
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. Binary I/O, also called buffered I/O, expects and produces bytes objects. Raw I/O, also called unbuffered I/O, is closer to the hardware. def reading_in_one_chunk(): f = open('AliceInWonderland.txt', 'r') s = f.read() f.close() ... s.count('!') ... ... s[0]) ... for k in range(10): print(s[k]) def writing(): 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). def reading_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.


Download ppt "Input and Output with FILES"

Similar presentations


Ads by Google