Python Windows open permission denied as adminstrator - python

I have simple python code on Windows that reads/writes a log file that I don't want standard users to have write access, so I have made the log files restricted to Adminstrator write privileges.
import os
l = open('filename.log', 'w')
I can't figure out how to debug it without running into a permission denied error. Debugging in PyCharm and even though starting PyCharm as admin, I get the error.
I can, however, run a python shell as admin and open/write my log file without error.
How can I run PyCharm to debug without getting the error?

After confirming that the PyCharm process is running with administrators group using accesschk as recommended by eryksun, the code is now executing without the error.

Related

Why am I getting PermissionError [Errno 13] when attempting to write to log in Flask hosted by Apache?

I am running Flask 1.1.4 via Python 3.5.3, hosted via an Apache 2 server on Debian Stretch. I am attempting to log various messages from the program, using the python logging module. This works normally. However, if I restart the Apache server using sudo service apache2 restart, the Flask application errors out with PermissionError: [Errno 13] Permission denied: (log file name). Is this an issue anyone else has run into?
If it is not a permissions issue, this usually happens (happened to me a few times) if you are using a relative path for the log file.
The current working directory for an app running with mod_wsgi is not the directory the the app is in. If you are using a relative path for the log file you can try to use absolute path instead.
More info here: https://modwsgi.readthedocs.io/en/master/user-guides/application-issues.html#application-working-directory
It turns out that there was a cron process that was editing the ownership of all files in my log folder to root. Once the program was restarted and needed to re-obtain the file reference, it was unable to do so.

Accessing the Host File In A Python Program

I created a program which modifies the hosts file in order to block some websites. But when I am running the programme, I am getting this error.
Btw I have created my Python file using Pycharm IDE and my intention is to run this script everytime open my PC using Task Scheduler. So please kindly do tell me what I should be running as the administrator. Is it like the Pycharm itself? Most importantly how do I give it Admin permissions permanently?
[Errno 13] Permission denied: 'C:\\Windows\\System32\\drivers\\etc\\hosts'
Please kindly tell me a way to fix this.
On Windows create a scheduled task on logon, and configure the task to run under a system account. The task could simply be run as a batch file and within the batch file you may run the below command. Ensure the task is set to run with administrator privileges in the task creation window
C:\PythonFolder\python.exe yourscript.py

Script runs fine, but when run by cron can't do the Dropbox upload

I'm running a python script on my raspberry pi, which makes some modifications in a SQL database, writes a log, and uploads everything to dropbox.
When I'm launching it using command line everything works fine.
UPDATE: When I'm launching it using cron, everything works, except for the Dropbox upload. No error messages in the log. The file simply doesn't appear in my dropbox.
Here is the code I am using:
from subprocess import call
data = "/home/pi/scripts/Dropbox-Uploader/dropbox_uploader.sh upload /home/pi/scripts/database.db /"
call ([data], shell=True)
How can this be fixed?
It works from an interactive terminal and not from cron is almost always an evidence of an PATH or environment problem. When you use an interactive session, the profile and eventually basrc files are used to set a number of environment variables including PATH. None of them are used from cron. So good practices are:
always use absolute path in scripts that can be launched from cron
explicitely set PYTHON environment variables in your crontab, or use a minimal shell to set them first and then start python

eb.exe deploy": Errno 13 Permission denied: './pagefile.sys'

I am compiling a website using TeamCity on a server and need to deploy the compiled website to AWS.
As my last build step, I use the Elastic Beanstalk CLI to deploy: "C:\Python34...\eb.exe deploy".
eb init has already been run...but whenever I run "eb deploy", (even when I run it from the command line in an empty directory--which should deploy a default project to AWS), an error appears saying:
Error: PermissionError :: [Errno 13] Permission denied: './pagefile.sys'
I have already run the command on my local machine without any problems; I receive the error on the server regardless of whether I am running the command line as an administrator.
I am wondering if this is a permissions issue with the server, or something else? I haven't been able to achieve much insight from the other questions, because they seem to have been solved on a case-by-case basis.
pagefile.sys is the Windows swap file.
It is a special file which cannot be written or manipulated. Whatever your command is doing you need to fix your command so it that doesn't touch this file and ignores it.

Permission to access network drives when running Python CGI?

I have a Python script running on the default OSX webserver, stored in /Library/WebServer/CGI-Executables. That script spits out a list of files on a network drive using os.listdir.
If I just execute this from the terminal, it works as expected, but when I try to access it through a browser (computer.local/cgi-bin/test.py), I get a permissions error:
<type 'exceptions.OSError'>: [Errno 13] Permission denied: '/Volumes/code/code/_sendrender/queue/waiting'
Is there any way to give the script permission to access the network when accessed via CGI?
I don't know much about the default osx webserver, but the webserver process is probably being run as some user, that user needs to be able to access those files. To find out who the user is you can use the ps command. Then depending on the configuration of the network shared drive, you can add this user to the users allowed to access this data.

Categories

Resources