使用Python脚本中的子进程执行curl语句不起作用

时间:2017-01-17 15:42:07

标签: python curl subprocess

我正在尝试从Python脚本运行以下curl语句,以便使用松弛API上传文件:

curl -F file=@test.csv -F channels=#general -F token=xxxx-xxxxxxxxx-xxxx https://slack.com/api/files.upload

当我从终端运行上述命令时,它成功将正确的文件上传到#general频道。但是,当我尝试使用下面的代码从我的Python脚本运行它时,我收到一条错误消息:

bash_cmd = 'curl -F file=@test.csv -F channels=#general -F token=xxxx-xxxxxxxxx-xxxx https://slack.com/api/files.upload'
process = subprocess.Popen(bash_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stout, sterr = process.communicate()

错误追溯:

Traceback (most recent call last):
File "trendbot.py", line 75, in <module>
handle_command(command, channel)
File "trendbot.py", line 47, in handle_command
process = subprocess.Popen(bash_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "/Users/melanie/anaconda/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/Users/melanie/anaconda/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

如果我打印bash_cmd并将其粘贴到我的终端窗口中,它也会成功运行。所有帮助赞赏。

1 个答案:

答案 0 :(得分:0)

回答我自己的问题:

我必须设置def dats_within_radius(R,x,y): return [[r_loop, X, Y] for r_loop,X,Y in dats if (X-x)**2+(Y-y)**2 <= R**2] ,因为我的shell=True是一个字符串。现在我运行以下命令并运行:

bash_cmd