PermissionError in Python script using io

Report issues, odd behaviors or submit a detailed bug report.
Post Reply
User avatar
pjj
Posts: 1653
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

PermissionError in Python script using io

Post by pjj » 16 May 2019 10:40

I'm starting my adventure with Python 8) and I want to run my scripts from within RJ TE. I have set up "run Python" as a tool and it works very well: output is written to the console and captured and displayed in the Messages panel; when a script uses Tk, window is shown etc. All nice and smooth. However, I have a problem with a script using 'os' module:

Code: Select all

#!/usr/bin/env python3
import os
file = open('testfile.txt', 'w')
file.write('Hello World')
file.close()
I'm getting this error:

Code: Select all

Traceback (most recent call last):
  File "X:\xxx\readfile.py", line 3, in <module>
    file = open('testfile.txt', 'w')
PermissionError: [Errno 13] Permission denied: 'testfile.txt'
Process completed with exit code: 1
When I run the same script from cmd, I get no error and testfile.txt is created as expected.

I thought the error was due to cmd running from within RJ TE without admin privileges; however, I just ran ConEmu (Windows terminal) without it and testfile .txt was created, so I don't have any more suggestions. This isn't super important by any stretch, but could you please have a look at it someday? Thanks!

Btw. I made similar script for PHP for testing purposes:

Code: Select all

$fp = fopen("X:\\xxx\\testfile2.txt", "w");
fwrite($fp, 'Hello, world!');
fclose($fp);
testfile2.txt is created without any problem, when test.php is run from within RJ TE.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

User avatar
Rickard Johansson
Site Admin
Posts: 5736
Joined: 19 Jul 2006 14:29

Re: PermissionError in Python script using io

Post by Rickard Johansson » 16 May 2019 13:53

I think you just need to specify a full path.

Code: Select all

#!/usr/bin/env python3
import os
file = open('c:\\foo\\testfile.txt', 'w')
file.write('Hello World')
file.close()
Or maybe use

Code: Select all

os.chdir('C:\\foo')
before you access any files.

User avatar
pjj
Posts: 1653
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: PermissionError in Python script using io

Post by pjj » 16 May 2019 17:24

This. What has confused me was the fact that script run from the console automatically assumed current path, i.e. the same as script's.

Thank you!
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

Post Reply