Cannot write to a file within python script, on startup - python

Basically I have a python script which is "converted" to exe, located in C:\Users\USER_NAME\AppData\Roaming\Folder. When started manually (not as admin), the script can write to file (file is located in the same directory as the script). But when the script runs on startup (I added a registry string to Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run with an executable path as data) I get Permission Denied error, why is that so and how could I fix it?. Code for opening file:
file = open(os.path.join(current_path, "text.txt"), "a+")
Error:
PermissionError: [Errno 13] Permission denied: '.\\text.txt'

The issue was that the script was run from C:\Windows\System32 not from C:\Users\USER_NAME\AppData\Roaming\Folder (windows being weird I guess)

Related

How to copy file through Shutil python

I was trying to copy file from my desktop to System32 dir. it give me the following error.
PermissionError: [Errno 13] Permission denied:'C:/Windows/System32/abc.exe'
here is my code
src = pth + "\\" +s_name
dist = "C:/Windows/System32/"
shutil.copy(src , dist)
The issue is not with Python or shutil. Permission denied means that the user that runs the Python script does not have a permission to copy the file to C:/Windows/System32.
I think what you could do now is to look for a way to run the script as Windows administrator.

PermissionError on writable file in Jenkins

I'm trying to open a file in python that was opening fine before with this syntax, but now that I've rearranged directories I am getting a PermissionError and I cannot pinpoint why. I have directory ahs_reports containing files create_ahs_report.py and message_body.html. ahs_report is contained in a directory called reports. message_body.html has permissions -rwxrwxrwx so it should be writable. ahs_report/ also has the same permissions. However, when I run create_ahs_report.py from directory reports, and get to this line
with open('message_body.html', 'w+') as f:
I get PermissionError: [Errno 13] Permission denied: 'message_body.html'. I feel like there's something obvious I am missing. What is it? I am running the script thru a Jenkins job as user jenkins and I've never had an issue like this.

execute a python file globally from any path - windows7

I have a script abc.py under C:\Python\Python35\Scripts\ folder and I have C:\Python\Python35\python.exe;C:\Python\Python35\Scripts;C:\Python\Python35; in my PATH. Still I'm not able to execute the python from anywhere else otherthan its directory.
For example, I always have to go to C:Python\Python35\Scripts and then execute python abc.py. Else, it is giving below error -
python: can't open file 'abc.py': [Errno 2] No such file or directory
Any help here is much appreciated.

Python 2.7; Downloaded a file and attempted to overwrite an .mp3, receiving: IOError: [Errno 13] Permission denied: 'audio.mp3'

Python 2.7
Windows 8
I downloaded a file from the internet using:
testfile = urllib.URLopener()
testfile.retrieve(str(audio_link), "audio.mp3")
audio_link is a format something like:
http://static.sfdict.com/staticrep/dictaudio/R02/R0203700.mp3
The program should be writing to an existing audio.mp3 in the working directory with the url from the above code. It was working properly until I made some changes to the program that don't seem to have to do with the problem.
Specifically, the problem shows:
File "D:\Paul\projects\spelling test\python.py", line 105, in get_pronunciation
testfile.retrieve(str(audio_link), "audio.mp3")#open the link as a saved file; creates file; maybe overwrites old??
File "C:\Python27\lib\urllib.py", line 249, in retrieve
tfp = open(filename, 'wb')
IOError: [Errno 13] Permission denied: 'audio.mp3'
get_pronunciation is a function, and I think that tfp is built in to the testfile usage.
My cmd has no chmod (I tried chmod 777 /working/directory/blah/) and, even as administrator, it is not recognized as a command.
I also tried to go to the properties of the working directory through windows explorer>right-click>properties>security tab>edit>click on Users>check-mark full control.
Help? Regards.

How do I zip and unzip files in python?

I am trying to move a folder from one place to another, however I get this error when I try:
PermissionError: [Errno 13] Permission denied
I use the following code:
import os.path, shutil #, zipfile
if os.path.exists("\\\BOGORODITSASERV\\Not.py Files"):
print("#YOLO")
#zip command
shutil.copy2('C:\\Users\\Grant\\Documents\\AllDerProgramming','\\\BOGORODITSASERV\\Not.py Files')
#unzip command
print("Success!")
EDIT: I have made sure that the file is not open/being used. So to get around this error I was thinking zipping and unzipping the file would work.
EDIT:How then, if not by zipping, do I get rid if this error? Can I give python permission to this file path?
EDIT:I run this program on an admin account, I even run the shell as administrator and I still get the permission error.

Categories