Computer Science 1026A/B Chapter Notes - Chapter 7: Delimiter, Escape Character, Newline

99 views1 pages
Chapter 7: Files & Exceptions
test files: include windows notepad, python source code, and html files
open a file: supply the file name and mode in which the file is to be opened
function >>> infield = open(“input.txt”, “r/w”) >>> chose r or w for either reading or writing
file object: object returned when open function is called
>>> stored in a variable
close a file: use the close method >>> infield.close()
fileName.readline() >>> obtains lines of text from a file
>>> only obtains strings, but can be converted to int/float; if converted “\n” will not be
printed as part of what the line contains (\n = new line)
write text to a file: fileName.write(“Hello, World!\n”) or print(“Hello, World!”, file=fileName) or
print(“Hello, World!”, end=“”, file=fileName)
\\ = \ >>> when entering the names of files since \ is an escape character on its own in python
iterate over a file object to read the lines of text in a file by using the for loop
unlike a container, once a file is read, you cannot iterate over the file again without closing
and then reopening it
line.rstrip() >>> removes the newline character (\n) from a line of text (no blank lines printed)
blank space = delimiter = white space
fileName.lstrip(chars) >>>characters in the string chars are removed
fileName.rstrip(chars) >>> characters in the string chars are removed from the right
fileName.strip(chars) >>>characters are removed from the front and end of s
line.split() >>> splits a string at each blank space and returns the list of substrings (arguments
can be passed in the brackets that will change where the string splits, rather than just spaces)
s.splitlines() >>> returns a list that contains lines of a string split using \n as a delimiter
fileName.read() >>> reads individual characters rather than a whole line (argument = # of ch to
read)
only reads and returns newline (\n) characters that terminate the individual lines when
encountered
programs that start from the command line receive the command line arguments in the argh
list defined in the sys module
exception handling: provides a flexible mechanism for passing control from the point of error
detection to a handler that can deal with the error
raise statement: signals an exceptional condition and raises an exception object
try block: contains statements that could cause an exception
except clause: the exception handler is placed in here
once a try block is entered, the statements in a finally clause are guaranteed to be executed,
even if an exception is not raised
raise an exception as soon as a problem is detected, and handle it only when it can be
handled
never use a finally block after an except block; they should never be apart of the same try
block
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 1 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Notes+

Unlimited access to class notes and textbook notes.

YearlyBest Value
75% OFF
$8 USD/m
Monthly
$30 USD/m
You will be charged $96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.