[Tutor] file(), open()
alan.gauld at btinternet.com
Sat May 26 15:50:00 CEST 2007
"Alan Gilfoy" <agilfoy at frontiernet.net> wrote in
>I want to work with simple ASCII text files, here..
You may find it worth reading the relevant section in one
of the tutorials. The official python one is good or you
could try the Handling files topic in mine. Most tutorials
will answer all of the questions you ask and give examples.
> I know you get them loaded into Python with file() or open()
You get access to the file objects, you don;t load anything
until you call one of the read methods (or use the file as an
> The arguments for file() and open() are: (filename, mode,
> How do you refer to the filename?
As a string
> Do you put it in quotes?
Yes, or store it in a variable and use that.
> Do you put in the file's full directory path?
Yes, or it will use the current working directory as shown
by os.getcwd() and can be changed using os.chdir()
> Or do file() and open() refer to the same directory
> as the one the script is in?
Thats often what the cwd is set to but not always.
> Are the mode and buffering things always necessary arguments?
the default mode is 'r' - ie read - if you want any other mode
then you must supply it. I've never actually used buffering,
the default will do in nearly all cases.
If you do help(open) or help(file) at the python prompt
you will get a lot of info including this sat the top:
file(name[, mode[, buffering]]) -> file object
That tells you that open/file() take a mandatory name,
an optional mode and an optional buffering (the ) and
that they return a file object ( the -> symbol)
Its worth getting to know this notation because it is
compact and accurate and is often diosplayed by IDEs
with code introspection capabilities(IDLE, Pythonwin,
SPE, PyCrust, Eclipse etc)
> For now, I just want to read/manipulate files, not write to them.
> What do file() and open() create?
file objects. Those objects expose a bunch of methods.
Those methods are alspo described when you do help(file)
at the >>> prompt. The >>> prompt is also a great place
to try out these things to see for yourself exactly how they
work, what kind of values to pass etc. Never underestimate
the power of the >>> prompt in building code, especiually
when its new features that you are using for the first time
or things you only use occasionally...
> Do you need to set them equal to
> some variable? Is one better than the other?
If you just want to slurp up the content of a file you can
bypass the file variable with something like:
data = file('myfile.dat').read()
Or if you only want to do a single pass over the lines of
the file use:
for line in file('myfile.dat'):
Otherwise you are better assigning to a variable. If
you do you should really put the open within a try/except
and close it in a finally as a metter of best practice:
myFile = open('myfile.dat')
Its also good practice to close files as soon as possible
to ensure you don;t lock other users out from accessing
> I want to create a list, where each list item is a
> line in the file.
If you only want to process the lines once you can use
a for loop directly as shown above. Otherwise use
the readlines() method. All of this and more is covered
in my tutorial.
Author of the Learn to Program web site
More information about the Tutor