如何在python脚本中打印对文件的subprocess.call的结果

如何在python脚本中打印对文件的subprocess.call的结果,python,windows,python-3.x,command,jira,Python,Windows,Python 3.x,Command,Jira,我有一个python脚本,在这里我调用了jiraapi,并从JIRA那里得到了一些东西,我想把它写到文件中 cmd中的这个命令工作正常 curl -D- -u username:password -X GET --data @file.json -H "Content-Type: application/json" http:URL >> output.json 但是,当我尝试在Python中执行同样的操作时,它并没有写入我的文件(直接进入我的“有问题”) 我也试着让它写一个变量的

我有一个python脚本,在这里我调用了jiraapi,并从JIRA那里得到了一些东西,我想把它写到文件中

cmd中的这个命令工作正常

curl -D- -u username:password -X GET --data @file.json -H "Content-Type: application/json" http:URL >> output.json
但是,当我尝试在Python中执行同样的操作时,它并没有写入我的文件(直接进入我的“有问题”)

我也试着让它写一个变量的内容,如下所示

curler = (subprocess.call('curl -D- -u username:password -X GET --data @file.json -H "Content-Type: application/json" http:URL'))

def write():
    name = 'output.json'

try:
    file = open(name, 'w')
    file.write(curler)
    file.close()

except:
    print('something is wrong')
    sys.exit(0)
write()
我正在使用Windows 7和Python 3

子进程。call()
获取参数列表,并仅返回被调用进程的退出状态。我认为您正在尝试将标准输出重定向到一个文件:

curl = ['curl', '-D-', '-u', 'username:password', '-X', 'GET', '--data',
        '@file.json', '-H', 'Content-Type: application/json', 'http:URL']
with open('output.json', 'w') as file:
    status = subprocess.call(curl, stdout=file)
subprocess.call(['curl', 'google.com'], stdout=open('myFileName', 'w'))
subprocess.call()。我认为您正在尝试将标准输出重定向到一个文件:

curl = ['curl', '-D-', '-u', 'username:password', '-X', 'GET', '--data',
        '@file.json', '-H', 'Content-Type: application/json', 'http:URL']
with open('output.json', 'w') as file:
    status = subprocess.call(curl, stdout=file)
subprocess.call(['curl', 'google.com'], stdout=open('myFileName', 'w'))

1-出现异常的原因是您将参数传递给子流程的方式。您应该给子流程一个参数列表,而不是一个字符串。假设您想使用curl下载google.com:

subprocess.call(['curl', 'google.com'])
2-subprocess.call返回退出代码,而不是输出。要将输出重定向到文件,请执行以下操作:

curl = ['curl', '-D-', '-u', 'username:password', '-X', 'GET', '--data',
        '@file.json', '-H', 'Content-Type: application/json', 'http:URL']
with open('output.json', 'w') as file:
    status = subprocess.call(curl, stdout=file)
subprocess.call(['curl', 'google.com'], stdout=open('myFileName', 'w'))

1-出现异常的原因是您将参数传递给子流程的方式。您应该给子流程一个参数列表,而不是一个字符串。假设您想使用curl下载google.com:

subprocess.call(['curl', 'google.com'])
2-subprocess.call返回退出代码,而不是输出。要将输出重定向到文件,请执行以下操作:

curl = ['curl', '-D-', '-u', 'username:password', '-X', 'GET', '--data',
        '@file.json', '-H', 'Content-Type: application/json', 'http:URL']
with open('output.json', 'w') as file:
    status = subprocess.call(curl, stdout=file)
subprocess.call(['curl', 'google.com'], stdout=open('myFileName', 'w'))

你真的需要执行shell命令吗?你可以使用
pycurl
requests
直接在python中进行调用,而无需进行外壳处理,这样处理返回的数据就容易多了。你能给我们看一下回溯,这样我们就可以看到实际的错误吗?看,我将尝试使用“requests”,如果我能做到的话,我会在这里发帖的。目前,我正试图弄清楚如何像CURL命令一样传递请求。谢谢你真的需要执行shell命令吗?你可以使用
pycurl
requests
直接在python中进行调用,而无需进行外壳处理,这样处理返回的数据就容易多了。你能给我们看一下回溯,这样我们就可以看到实际的错误吗?看,我将尝试使用“requests”,如果我能做到的话,我会在这里发帖的。目前,我正试图弄清楚如何像CURL命令一样传递请求。谢谢我正在改变路线(使用请求),但这似乎是解决我问题的正确方法,所以我会这样做。非常感谢。Windows
CreateProcess
采用命令行字符串,而不是POSIX样式的参数列表或参数数组。在Windows上传递列表的唯一原因是为了跨平台兼容性,并让
Popen
根据VC++解析规则引用命令。在本例中,该命令已正确引用,例如:
subprocess.list2cmdline(curl)=
'curl-D--u username:password-X GET--data@file.json-H“Content Type:application/json”http:URL'
。所需要做的只是传递
stdout=file
以重定向其标准输出。感谢您让我知道,我没有windows机器可供测试。虽然我倾向于支持可移植性,所以我仍然会在Windows计算机上使用列表。我正在更改路由(使用请求),但这似乎是解决我问题的正确方法,因此我将这样做。非常感谢。Windows
CreateProcess
采用命令行字符串,而不是POSIX样式的参数列表或参数数组。在Windows上传递列表的唯一原因是为了跨平台兼容性,并让
Popen
根据VC++解析规则引用命令。在本例中,该命令已正确引用,例如:
subprocess.list2cmdline(curl)=
'curl-D--u username:password-X GET--data@file.json-H“Content Type:application/json”http:URL'
。所需要做的只是传递
stdout=file
以重定向其标准输出。感谢您让我知道,我没有windows机器可供测试。虽然我倾向于支持可移植性,但我仍然会在Windows计算机上使用该列表。