0

I have a script that has 4 lines of code:

import subprocess

folderAdd = r"D:\Program Files (x86)\someapp\"

result = subprocess.run(['"' + folderAdd +'someProgram.exe"','"' + folderAdd +'somefile.ext"'], stdout=subprocess.PIPE)
print(result.stdout)

I've edited the code slightly to remove the specifics of the files/folders (since I assume that's not the issue?). The someProgram.exe is a go lang program I've made, the somefile.ext is a file that I pass to the go land program using the command line (eg the syntax in command line is: "someProgram.ext somefile.ext". The issue I'm having is when I run this script (which is stored on my E drive - so that's the working directory) I get the following error:

PermissionError: [WinError 5] Access is denied

I've tried running this python script from within spyder (my ide of choice) and from command line. Both of which I've tried running as administrator (based on this question/answer). I still get the same permission error. Is there any other way around this?

user6916458
  • 400
  • 2
  • 7
  • 19
  • why are you adding '"' before folderAdd ? – Cyril Jouve Aug 29 '20 at 21:20
  • here they mentioned all possible solutions https://techisours.com/winerror-5-access-is-denied-fixed-completely/ enjoy –  Aug 29 '20 at 22:03
  • @CyrilJouve I'm adding the " because the folder address has a space in it (program file, rather than programfiles) so when I use it in cmd it would fail! – user6916458 Aug 29 '20 at 23:49
  • @Cyber-Tech thanks for the link, but after going through each suggestion I still have the issue. I can print out the command using print(...) and copy and paste it into the cmd and it works, so I'm confident the path etc. is correct. I'm on windows so chmod doesn't apply. – user6916458 Aug 30 '20 at 00:05

1 Answers1

1

you're adding double quotes that should not be here.

Also you should join path using python's facility : os.path.join (https://docs.python.org/3.8/library/os.path.html#os.path.join)

If you only what stdout, you can use subprocess.check_outout (https://docs.python.org/3.8/library/subprocess.html#subprocess.check_output)

import subprocess

folderAdd = r"D:\Program Files (x86)\someapp"

print(subprocess.check_output([os.path.join(folderAdd, 'someProgram.exe'), os.path.join(folderAdd, 'somefile.ext'])
Cyril Jouve
  • 990
  • 5
  • 15
  • Did you test this? When I tried it threw up an error because there's a space in the folder address – user6916458 Aug 30 '20 at 13:15
  • I don't have your program, but the following is working: `subprocess.check_output(os.path.join('C:\Program Files\Mozilla Firefox', 'firefox.exe'))` and it contains 2 spaces – Cyril Jouve Aug 30 '20 at 13:36
  • typo in previous comment : `subprocess.check_output([os.path.join('C:\Program Files\Mozilla Firefox', 'firefox.exe')])` – Cyril Jouve Aug 30 '20 at 13:46