0

I have a Python(Version 2.6.6) script like Below:

import subprocess

id = 834
urllink = "https://xyzm:8443/rest/import-job/" + str(id)
subprocess.call([
    'curl',
    '-k',
    '-u',
    'xxxx:abc',
    '-X',
    'GET',
    urllink
])

It returns some JSON output to the terminal. How can I redirect the output to a file, so that I can parse the file and use the same file(data) while executing a POST command?

Any reply would be greatly appreciated.

Thanks, Jee

3
  • 2
    Is there a reason you can't just use bash directly: curl -ku xxxx:abc -X GET https://xyzm:8443/rest/import-job/834 >yourfile.txt? Commented Apr 5, 2017 at 20:48
  • 1
    Possible duplicate of Redirecting subprocess stdout Commented Apr 5, 2017 at 21:06
  • I need to parse the json file and again upload the file to different userName just like copy. I can use bash script, but there are lots files and involves various logics, so I thought using python script would be a little bit easier. Commented Apr 6, 2017 at 0:38

3 Answers 3

1

The following code will write to a file in python 2.6 (I have it set to write what is contained in your output variable), followed by a read of that same file & then the print command will display the output on the terminal.

# Write a file
out_file = open("test.txt", "w")
out_file.write(output)
out_file.close()

# Read a file
in_file = open("test.txt", "r")
text = in_file.read()
in_file.close()

print text
Sign up to request clarification or add additional context in comments.

3 Comments

Always use a with block when dealing with files.
There is nothing in the output variable since the process returns the output to the terminal. That's why I am looking for the way to capture the output to a file.
Are you calling this from a terminal, or command prompt? In which case I would advise calling it like this to write to out.txt: python file.py > out.txt
0

Using PyCurl is easy.

Here's an example to get you started:

https://github.com/pycurl/pycurl/blob/master/examples/quickstart/get.py

For some options (setopt) you could get a hint using the --libcurl option of curl.

Comments

0

Thank you very much for taking time to reply.

I achieved the goal by using Python requests package without using Linux subprocess module or commands.

Once again, Thank you all!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.