jeudi 26 mars 2015

Passing string as argument adds escape characters

I am accessing the subprocess module to call a shell function. Part of the function call is a string:



data = '\'{"data": [{"content": "blabla"}]}\''


when passing the string, I get the following error:



from subprocess import check_output
check_output(['curl', '-d', data, 'http://ift.tt/1Nfyvdr'], shell=True)
Error: raise CalledProcessError(retcode, cmd, output=output) ... returned non-zero exit status 2


I actually know the problem, it's that the string gets passed the way it looks to Python, escapes and all.


Using the console,



$ curl -d \'{"data": [{"content": "blabla"}]}\' http://ift.tt/1Nfyvdr


gives the same error, while



$ curl -d '{"data": [{"content": "blabla"}]}' http://ift.tt/1Nfyvdr


runs perfectly. Any ideas how to tell Python that it passes a string .. fully converted?


Aucun commentaire:

Enregistrer un commentaire