Opening a file in Python using windows.

Ishwor ishwor.gurung at gmail.com
Sat Dec 4 12:09:47 EST 2004


On 3 Dec 2004 08:38:37 -0800, ed <ed_tsang at yahoo.com> wrote:
> I do have  permission toa ccess the file as that file is careted and read my me.
> I have read/write access to that location. regards

That shouldn't be really hard. :)
okay imagine you have a file called test.txt in C:\Python24\
now go to the IDLE prompt if you have one or just use Python prompt.
I am assuming you know what they are & how to do them. :) If you don't
then go read some books. :)

There  are two basic in-built operations to open a file.
The first one is open & the the obvious other one is called file.
file is just an alias for open.

type these lines in the prompt

>>> open("C:\\Python24\\test.txt",'r').readlines()
['This is test file. :)']

So thats there.. the whole file in a line :).. However remember that
it returns a list for further processing if you want to or just forget
it. if you break up those above line into 2 lines then they become

>>> test = open("C:\\Python24\\test.txt",'r') # read-only
>>> test.readlines()
['This is test file. :)']

So there you have it. The power of Python. The first line opens a file
object & places
the output at certain address in the computer memory & test
*references* that address. The second line (test.readlines()) reads
the actuall stream of bytes from that location. :)

> > If you do not have permission to access the files, switching to a
> > different language will not help you.  You need to determine the cause
> > of your access errors first.

Agreed but the OP does have permission so this assumption doesn't hold!

[snip]


-- 
cheers,
Ishwor Gurung



More information about the Python-list mailing list