-2

I have been trying to create a simple file using the below code.But I get the error message again and again. I have the full control over the python directory.

myfile = open('sri.txt','w')
myfile.write("My first line written in python \n")
myfile.write("Hello World")
myfile.close()

I get the following error message; Message File Name Line Position
Traceback
11
IOError: [Errno 13] Permission denied: 'sri.txt'

Please help me to sort out this. I am using windows 7 & python 3.2

  • 1
    This is rather a Windows permission question. Your Python code seems just fine to me. – Alfe Apr 05 '15 at 23:46
  • You could improve it by using the `with` statement of Python, though ;-) – Alfe Apr 05 '15 at 23:47
  • https://stackoverflow.com/questions/3740152/how-do-i-set-chmod-for-a-folder-and-all-of-its-subfolders-and-files-in-linux-ubu or https://www.linux.org/threads/file-permissions-chmod.4124/ can help in changing file permission – Kallz Aug 25 '17 at 12:44

4 Answers4

4

Well, you don't have write permission for that file. If the file already exists, you might not be able to overwrite it. It might also be that you don't have permission to write in that directory,

mhawke
  • 84,695
  • 9
  • 117
  • 138
0

I think you might be able to fix your problem by changing this:

myfile = open('sri.txt','w')

to this:

myfile = open('C:\\PATHTODIRECTORY\\sri.txt','w')

0

You could try running the python code with admin privileges and see if anything changes. If that doesn't work then you could try accessing another text file. It could be possible that the rights on sri.txt are a little wonky.

-1

Make sure the text file in the folder (version written by previously running the code) is closed. If the text file is open, python doesn't have the permission to overwrite/save.

Spica
  • 127
  • 2
  • 10