环境-PyCharm
我使用以下命令来获取 JSON 文件:-
aws rds describe-db-cluster-snapshots > snapshotdetails.json
我正在使用这个 Json 文件来提取一些数据。我希望从我的 Python 脚本运行上述命令。我已经尝试过以下但失败了:-
from subprocess import call
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.json"])
我收到错误并且它不起作用。有什么指点吗??
错误:-
Traceback (most recent call last):File "/Users/PrashastKumar/Desktop/CrossRegion/venv/lib/crossregiontrial.py", line 3, in <module>
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.txt"])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
答案1
一个python3
办法:
from subprocess import Popen, PIPE
cmd_output = Popen(["echo", "foo"], stdout=PIPE)
with open('bar.txt', 'w') as out_handle:
out_handle.write(cmd_output.communicate()[0].decode('UTF-8'))