Start New Discussion within our Software Development Community

I have some problems just to write a simple string output to some txt.file.
That is my test code:

test = open('Test.txt', 'w')

I use Python 2.6.5 and PyScripter

If I direcly enter the code in the IDLE SHELL, sometimes it works, sometimes only the file is generated and sometimes I get this Error message without changing anything in the Code.

>>> test = open('Test.txt', 'w')

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    test = open('Test.txt', 'w')
IOError: [Errno 13] Permission denied: 'Test.txt'

I am working with windows 7, could this be some operating system configuration problem?
I have asolutly no idea about this matter.


What is your current working directory when you are running the program? To find out, run the following commands from whatever editor you are going to use to test your program.

import os
print os.getcwd()

I suspect that you don't have all the necessary permissions to create and write to a file in the current working directory when you get the IO error. You can avoid the problem by including the path to a directory where you know you can create and update files, in your filename, like this:

>>> test = open('/home/david/Programming/Python/Test.txt', 'w')
>>> test.write('Hi there!')
>>> test.close()
This question has already been answered. Start a new discussion instead.