open does this.
open is a file name or a path to
a file.
If your're reading from the file, the file has to exist for this to work.
>>> mydata = open('data.txt')
Traceback (most recent call last):
File "", line 1, in
mydata = open('data.txt')
IOError: [Errno 2] No such file or directory: 'data.txt'
'r' means read, 'w' means write, and 'a' means append,
that is, write by adding to the file rather than writing over what is already there if the file already exists.
'r+' means that you can both read from and write to the file (by overwriting, not appending).
'r' is the default for the second argument, and you can leave it out if you only want to read from the file.
'utf8' works in most cases.
>>> español = open('es.txt', encoding='utf8')
close.
The following opens a file to write to, does nothing with it, and then closes it.
>>> data_file = open('data', 'w')
>>> data_file.close()
write, which takes a string as its argument.
Include the newline escape character '\n' at the end if you want a newline.
>>> data_file = open('data', 'w')
>>> data_file.write("Here's a line of data.\n")
>>> data_file.write("OK, that's all.\n")
>>> data_file.close()
print function, passing it file=stream, where stream is bound the stream object. The following does the same thing as in the previous bullet.
>>> data_file = open('data', 'w')
>>> print("Here's a line of data.", file=data_file)
>>> print("OK, that's all.", file=data_file)
>>> data_file.close()
with ... as form.
The following does the same thing as what's in the last two bullets.
>>> with open('data', 'w') as data_file:
print("Here's a line of data.", file=data_file)
print("OK, that's all.", file=data_file)
>>> phish('phish_data')
>>> phish('phish_data')
You're bank accownt will soon gonna closed; please to cooparate that it continu.
What is your name? William Jones
What is your SS#? 383091055
What is your bank account PIN? 3911
Thank you; everything in order!
for loop with the file object where a sequence would be expected. This binds the loop variable to each of the lines in the file, one at a time.
>>> phish_file = open('phish_data')
>>> for line in phish_file:
print(line, end='')
William Jones
383091055
3911
>>> phish_file.close()
Note that end='' is required to prevent an extra newline character at the end of each
printed line.
read returns the contents of the file as one long string.
Also note that, as with write streams, you can use the special with...as form to avoid having
to close the stream.
>>> with open('data') as phish_file:
phish_file.read()
'William Jones\n383091055\n3911\n'
readline returns a single line from the file.
Note that a stream object maintains a pointer that keeps track of what point in the file reading has progressed to.
>>> phish_file = open('phish_data')
>>> phish_file.readline()
'William Jones\n'
>>> phish_file.readline()
'383091055\n'
>>> phish_file.readline()
'3911\n'
>>> phish_file.readline()
''
>>> phish_file.close()
>>> español = open('es.txt', encoding='utf8')
>>> español.readline()
'Buenos días.\n'
>>> español.readline()
'¿Cómo estás?\n'
>>> español.readline()
'Adiós.\n'
>>> español.close()
split, replace, and
isdigit
(see the documentation on string methods).
>>> get_SS('stuff.txt')
[383091055, 983901100, 767389099]
Answers to problems in these notes