Python: Subprocess “call” function can't find path

[亡魂溺海] 提交于 2021-02-08 11:31:25

问题


I have a python program that needs to run a bash command on a server. The program works when I run a bash command in my local directory like this:

import subprocess from subprocess import call call(['bash', 'Script_Test.sh'])

However, after SSH'ing to a server and running a similar line of code below with a path on the server to a bash script, I get the error "No such file or directory"

call['bash', path]

This doesn't make sense for a number of reasons. I triple checked that the path is correct. I went on Putty, connected to the server on there, and ran a bash command with the same path and it worked, so it can't be the path. I also ran a number of tests to make sure I was SSH'd into the correct server, and I was. I thought there was a security issue on the server with me running bash, so I tried cat instead. Nope, still unable to locate the path.

I'm not super familiar with python subprocesses, so any pointers to anything I'm missing here with "call" would be very helpful.


回答1:


Making Sure Your Script is Ready for Execution

  1. Give your script a shebang line

First things first, it is important that you include a shebang line in your script on Unix-like systems. I recommend, for your script's portability, that you use #!/usr/bin/env bash.

A Note on File Extensions:

I would recommend that you remove the .sh extension from your script. If you are using the Bourne Again Shell (bash) to execute your script, then using the .sh extension is misleading. Simply put, the Bourne Shell (sh) is different than the Bourne Again Shell (bash) - so don't use a file extension that suggests you are using a different shell than you actually are!

It's not the end of the world if you don't do change your file extension - your script will still be executed as a bash script if you have the proper bash shebang line. Still, it is good practice to either use no file extension (Script_Test -- strongly preferred) or the .bash file extension (Script_Test.bash).

  1. Give your script the proper file permissions

For your purposes, maybe it is only important to give the current user permissions to read and execute the script. In that case, use chmod u+x Script_Test.sh. What is important here is that the correct user (u+) / group (g+) has permissions to execute the script.

  1. Make sure that your script's path is in the $PATH environment variable

Executing your bash script in a Python script

Once you've followed these steps, your Python script should work as you've called it in your question:

import subprocess
from subprocess import call

your_call = call("Test_Script.sh")

If you would rather not move your script into the $PATH environment variable, just make sure that you refer to the script's full path (which is the current directory, ./, in your case):

import subprocess
from subprocess import call

your_call = call("./Test_Script.sh")

Lastly, if your script does not have a shebang line, you will need to specify an additional parameter in your call function:

import subprocess
from subprocess import call

your_call = call("./Test_Script.sh", shell=True)

However, I would not recommend this last approach. See the Python 2.7.12 documentation for the subprocess package...

Warning: Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.

Please check out @zenpoy's explanation to a similar StackOverflow question if you are still having problems.

Happy coding!



来源:https://stackoverflow.com/questions/38231176/python-subprocess-call-function-cant-find-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!