open
function is the preferred method for reading files of any type, and probably all you'll ever need to use. Let’s first demonstrate how to use this method on a simple text file.open
method, we'll have our file saved as an object.f
, Python tells us the status (open or closed), the name, and the mode, as well as some info we don't need (about the memory it's using on our machine).f
is in mode r for read. Specifically, this means we can only read data from the file, not edit or write new data to the file (it's also in t
mode for text
, though it doesn't say this explicitly —it's the default mode, as is r). Let's read our text from the file with the read
method:2 | 'First line of our text.nSecond line of our text.n3rd line, one line is trailing.n' |
n
newline characters, we can print it):2 4 | First line of our text. 3rdline,one line istrailing. |
_
character in the Python IDLE to reference the most recent output instead of using the read
method again. Here's what happens if we try to use read
instead:2 | ' |
read
if you don't want the full contents of the file; Python will then read however many bytes you specify as an integer argument for read
.seek(int)
method on f
. By going back to the start you can read the contents from the beginning again with read
:2 4 | # We only read a small chunk of the file, 10 bytes First line |
tell
method on f
like so:2 | 10L |
readline
or readlines
methods—the first reads one line at a time, the second returns a list of every line in the file; both have an optional integer argument to indicate how much of the file (how many bytes) to read:2 4 6 8 10 | >>>f.seek(0) ['First line of our text.n','Second line of our text.n','3rd line, one line is trailing.n'] 'First line of our text.n' 'Second line of our t' # Note if int is too large it just reads to the end of the line 'ext.n' |
2 4 6 | >>>forline inf: First line of our text. 3rdline,one line istrailing. |
readlines
method to access a specific line in the file.test.txt
, containing the following text:2 4 | two four |
2 4 6 | >>>test_lines=test_file.readlines() >>># Print second line two |
print
adds another line ending. You can use the strip method, such as print(test_lines[1].strip())
.open
.truncate
method.f
and open a new one f2
:2 | >>>f.close() |
f
file is now closed, meaning it isn't taking up much memory, and we can't perform any methods on it.close
explicitly on the file, you can use a with
statement to open the file. The with
statement will close the file automatically:2 4 6 8 | >>>withopen(tf)asf: First line of our text. 3rdline,one line istrailing. >>>f.closed |
write
:writelines
, which will write a sequence (e.g., a list) of strings to the file as lines:2 | f2.writelines(['And a fifth','And also a sixth.']) |
writelines
is a misnomer, as it does not write newline characters to the end of each string in the sequence automatically, as we'll see.f2
so the changes we've made should be seen in the file when we open it in our text editor:writelines
method didn't separate our fifth and sixth lines for us, so keep that in mind.