Search code examples
pythonpython-2.7downloadshpermission-denied

When attempting run a python script from within another python script, I get 'permission denied'!


So, I was writing a python script, and it automatically downloads an update at the beginning of the script. Ok, so? So when I try to overwrite it, it gives me sh: /Users/<user>/Desktop/<scriptname>.py: Permission denied using CodeRunner 2.0.2, IDLE 2.7.9, and PythonLauncher. Here's the affected snippit:

cwd = os.getcwd()
cwd = (str(cwd) + "/<scriptname>.py")
update = urllib.URLopener()
cwd = str(cwd)
print "Downloading Update..."
update.retrieve("http://<site_domain>/<scriptname>.py",cwd)
time.sleep(1.25)
print "Update Dowloaded! Please Wait..."
time.sleep(2.5)
os.system(cwd)

I found this odd since people say that it should overwrite no problemo, however, I found it odd when there was no brother .pyc, meaning it may have been attempting to read and write at the same time, not to mention it worked fine, then suddenly went kaput.


Solution

  • os.system(cmd) will call [current directory]/<scriptname>/py.

    Instead of calling the script outright (it may not be executable), pass it to Python:

    cwd = os.path.join(os.getcwd(), "<scriptname>.py")
    ...
    os.system('{} {}'.format('python', cmd))