Reading/Writing files

Jack Trades jacktradespublic at gmail.com
Fri Mar 18 18:03:50 EDT 2011


On Fri, Mar 18, 2011 at 4:59 PM, Jack Trades <jacktradespublic at gmail.com>wrote:

>
>
> On Fri, Mar 18, 2011 at 4:56 PM, Jon Herman <jfc.herman at gmail.com> wrote:
>
>> Jack,
>>
>> thanks.
>>
>> Alright, so what I did is create a file called hello.txt with a single
>> line of text in there. I then did the following:
>>
>> f="fulldirectory\hello.txt" (where fulldirectory is of course the actual
>> full directory on my computer)
>> open("f", "w")
>>
>> And I get the following error: IOError: [Errno 13] Permission denied: 'f'
>> If I open to read, I get: IOError: [Errno 2] No such file or directory:
>> 'f'
>>
>> Can anyone explain to me why this happens?
>>
>>
> You need to remove the quotes from f.  f is a variable name, not a string,
> so there should be no quotes around it.
>
>
> --
> Jack Trades
> Pointless Programming Blog <http://pointlessprogramming.wordpress.com>
>
>
Sorry I should have given an example.  From your code it should look like
this...

filename = "fulldirectory\hello.txt"

You then use f to write or read.  This is how you read...

f = open(filename, "r")
file_contents = f.read()
f.close()

and this is how you write...

f = open(filename, "w")
f.write("hello world")
f.close()

-- 
Jack Trades
Pointless Programming Blog <http://pointlessprogramming.wordpress.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110318/c27bd2a3/attachment.html>


More information about the Python-list mailing list