Files

A place where you can post Python-related tutorials you made yourself, or links to tutorials made by others.

Files

Postby metulburr » Wed Feb 13, 2013 10:31 pm

Files

The built-in function open() creates a Python file object, which serves as a link to a file residing on your machine. After calling open(), you can transfer strings of data to and from the associated external file by calling the returned file objects's methods.

Code: Select all
f = open('test.txt' ,'w')
f.write('some text')
f.close()
This is the basic layout of creating a file. In this example, f is the file object from the builtin method open(). 'test.txt' is the name of the file we are creating and 'w' is the mode we are using (w = write).And finally we close the stream to the file with f.close().

Code: Select all
f = open('test.txt')
filer = f.read()
f.close()
print(filer)
In this example we are reading the previous example's file. We create the object f with open(). 'test.txt' is the file was are reading. This time there is no mode as open() without a mode default's to read, which you can put 'r' (r = read). f.read() takes in the entire file and assigns it to filer. Then we close the stream to the file and print the variable filer.

Modes
In the above example we used 'w' mode to write to the file. There are however different modes we can use. 'w' is write to. 'r' is read, which by having nothing as mode defaults to 'r'. 'a' is append to the file. 'rb' is read bytes. 'wb' is write bytes. Adding a '+' to either the input or output mode you can read and write to the same file object.

File Object Methods
file.readlines()
f.readlines() will read the entire file with each line as a index of a list. The first line being the first index and so on.
Code: Select all
f = open('test.txt' ,'w')
f.write('from the first line \nfrom the second line \nfrom the third line')
f.close()

f = open('test.txt')
filer = f.readlines()
f.close()
print(filer)

You can then if needed strip off the newline with the builtin function strip() upon iterating the list.
Code: Select all
['from the first line \n', 'from the second line \n', 'from the third line']


file.readline()
f.readline() will read the next line into the string including the newline character.
Code: Select all
f = open('test.txt' ,'w')
f.write('from the first line \nfrom the second line \nfrom the third line')
f.close()


def next_line(f):
    return f.readline()
   
f = open('test.txt')

print(next_line(f))
print('a')
print(next_line(f))
print('b')
print(next_line(f))
print('c')

The output below shows each line being called along with its newline character which is why there is a blank space. Then after each line we have out regular print statement. The c is not printed after a newline because the thrid line in the file is the last line, which does not have a newline in it, thus there is not newline printed between the two.
Code: Select all
m the first line

a
from the second line

b
from the third line
c


file.read()
f.read() will take the enitre file into a string, separating each line with '\n' (the newline).
Code: Select all
>>> f = open('test.txt', 'w')
>>> f.write('some random data\nother random data')
34
>>> f.close()

>>> f = open('test.txt')
>>> f.read()
'some random data\nother random data'
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1473
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Files

Postby mouseroot » Sun Feb 17, 2013 3:33 pm

instead of using the readlines() method you can use the
"for ... in" operator to loop through all the lines in a file
Code: Select all
program = open("main.py","r")
for line in program:
    print line
mouseroot
 
Posts: 17
Joined: Sat Feb 16, 2013 1:14 am

Reading sequential files

Postby KevinD » Tue Mar 12, 2013 1:00 am

Alright, this isn't really a tutorial in the truest sense of the word. It is, however, something I discovered the hard way and I'd like to share.

I was working on a program to process a large text file. I needed to read every line, so of course the obvious logic is something like this:
Code: Select all
for line in file_in:
    process_line(line)

However, depending on what was in the line, I wanted to do something specific with the next few lines. So, I changed my code, like this:
Code: Select all
for line in file_in:
    process_line(line)
    if 'certain text' in line:
        next_line = file_in.readline()
        process_second_line(next_line)
        next_line = file_in.readline()
        process_third_line(next_line)

The logic was sound, but the code obviously wasn't. For some reason, the contents of 'next_line' were not what I had expected - they were actually text from a few lines further into the file.

A bit of research (and a very helpful colleague) taught me that when Python is looping through a text file using a "for" loop, it is actually using a read-ahead buffer. When I used the line "for line in file_in:", the value of "line" only went as far as the first line feed character, but the next chunk of my file was already buffered, waiting for the next iteration of the loop. When I used the "readline()" method on my file, I was reading from the end of that buffer to the next line feed, and losing lots of text.

It turns out that I should have used the "next()" method instead of "readline()". The "next()" method reads from the previous line feed up to the next line feed, which is what I wanted. So, the proper code was something like this:
Code: Select all
for line in file_in:
    process_line(line)
    if 'certain text' in line:
        next_line = file_in.next()
        process_second_line(next_line)
        next_line = file_in.next()
        process_third_line(next_line)


Yes, I know I could have set a flag if "certain text" was in my line, and processed it differently on the next loop iteration, but that made the code a bit less clean than I wanted, and to be honest, it was a bit less intuitive, too.

As I said at the start, this isn't really a tutorial, but something I learned the hard way that I thought I'd share in case anyone else runs into a similar problem. If it helps you, you're welcome. If it doesn't, well, thanks for reading anyway.
Last edited by KevinD on Tue Mar 12, 2013 1:32 pm, edited 1 time in total.
Reason: merged topic into the "files" tutorial topic
Quanto lignum posset materiae materietur marmota Chuck si materiam possit materiari foedans, penitus lignum?
KevinD
 
Posts: 30
Joined: Fri Feb 08, 2013 3:15 am

Re: Reading sequential files

Postby micseydel » Tue Mar 12, 2013 3:05 am

Thanks for sharing! I've gone through this as well, and I think the way you're doing it is definitely better than a flag and all.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1372
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Files

Postby bunburya » Thu Nov 14, 2013 12:26 am

Instead of this:
Code: Select all
>>> f = open('test.txt', 'w')
>>> f.write('some random data\nother random data')
34
>>> f.close()


it is much more advisable to do it like this:

Code: Select all
>>> with open('test.txt', 'w') as f:
...     f.write('some random data\nother random data')
...
34


You'll notice that I didn't have to explicitly say "f.close()" here. That's because, if I use the "with open" construct, Python closes it for me whenever I leave that indented block of code. This is not only more convenient, but it's also safer, because it means that if an exception is raised while reading from or writing to the file, Python will automatically close the file before continuing, whereas if you do it the other way, the file could be left open in the event of an exception being raised.
http://www.bunburya.eu | https://github.com/bunburya

Join the #python-forum IRC channel on irc.freenode.net!
bunburya
 
Posts: 9
Joined: Sat Feb 09, 2013 7:28 pm
Location: Dublin, Ireland


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests