python在不同的目录中执行远程脚本

时间:2016-11-19 01:21:15

标签: python bash shell

在提示输入用户名和密码

后写下要在下面执行的python脚本
./3rdpartyscript -u username -p password -flag 123

它工作得很好,但是" 3rdpartyscript"有一堆它依赖的文件,我不想弄乱根目录,所以我将所有内容都移到了一个文件夹中。我也没有实际操作脚本的权限,只能运行它。

我无法正常运行:

./folder/3rdpartyscript -u username -p password -flag 123
OR
/folder/3rdpartyscript -u username -p password -flag 123

我也会将此脚本的结果用于其他事情。

这是我到目前为止所拥有的:

6 def getCreds():
  7         global access_key, secret_key, yourName
  8         access_key = raw_input("Enter User Name: ")
  9         secret_key = raw_input("Enter Password: ")
 10         infoCorrect = raw_input('Is this information correct? (y or n)')
 11         if infoCorrect.lower() == "yes" or infoCorrect.lower() =="y":
 12                 p = subprocess.Popen("./3rdPartyScript -u %s -p %s -flag 123" % (access_key, secret_key), shell=True, stdout = subprocess.PIPE)
 13                 output,err = p.communicate()
 14                 print(output)

I'll take the result of "output" and eventually put it against some kinda decision. 

 17         else:
 18                 print "\n Couldn't connect to please check your credentials \n"
 19
 21
 22 getCreds()

如何从远程目录运行此脚本?

由于

1 个答案:

答案 0 :(得分:1)

使用cwd参数:

 p = subprocess.Popen("./3rdPartyScript -u %s -p %s -flag 123" % (access_key, secret_key), 
                      shell=True, 
                      stdout=subprocess.PIPE, 
                      cwd=YOUR_DIRECTORY)