-2

I am writing a small python script, where I also want to execute some bash commands by using os.opopen(). I want to save the output to file but it is not working. I have used the ">" before and it always works.
My code:

curl https://r2---sn-4g5e6ne6.googlevideo.com/videoplayback?key=yt6&txp=5432432&mime=video%2Fwebm&pl=16&source=youtube&gir=yes&signature=6A07D1659991EC9A570BC6A0E9C10FB54E743DBC.DA7798D03282620987FC2FBDB020D3FE464851F3&fvip=2&requiressl=yes&sparams=aitags%2Cclen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cexpire&ei=sTFcXPqmN9PlgAeN6ZiYCQ&itag=247&ipbits=0&mm=31%2C26&mn=sn-4g5e6ne6%2Csn-i5heen7l&c=WEB&id=o-AHSObPeCuNcs7m1xagNdnC7zcHOvQEKkCrV9NDTw7g7a&initcwndbps=1722500&ip=132.187.12.151&clen=64580535&mt=1549545753&mv=m&dur=561.594&ms=au%2Conr&keepalive=yes&expire=1549567506&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278&lmt=1541003190670111&ratebypass=yes > test.bin

The bash command on its own works as expected.

Python command that I use:

os.popen("curl "+url+" > "test.bin") #url predefined

Any ideas where the problem could be?

CroatiaHR
  • 615
  • 6
  • 24
  • what have you tried? what is the python code? have you read the docs for the subprocess module? – Mihai Andrei Feb 07 '19 at 14:03
  • Yes, I have an idea... I bet it is in your python code. The one you talk about but don't show for us to help :-( The curl invocation that you show is working, right? – Poshi Feb 07 '19 at 14:04
  • If you are running this with Python, please post that code. This is bash - if you are running this line of bash alone, please remove the Python tag. – Jordan Singer Feb 07 '19 at 14:04
  • sorry guys, in the rush I have posted just the bash code I will update – CroatiaHR Feb 07 '19 at 14:06
  • Quote your URL, it contains the `&` character which sends a job to the background: `curl 'https://www.example.com?a=b&c=d'` – Benjamin W. Feb 07 '19 at 14:08
  • But you say "the Bash command on its own works as expected", which is *not* what I'd expect. – Benjamin W. Feb 07 '19 at 14:09
  • @BenjaminW. the quotes remark was a good one, but I overreacted when I thought that it will solve the problem. It now doesnt show the output in the terminal, but it is still not saved to a file – CroatiaHR Feb 07 '19 at 14:11
  • Have you tried running this command in a terminal shell, without Python? I suspect this isn't a Python problem. – Jordan Singer Feb 07 '19 at 14:13
  • yes I have.. in the terminal it works... when I execute the command on its own it works, but when I try to save the output it stopped. The reference from Benjamin was a good one – CroatiaHR Feb 07 '19 at 14:15

2 Answers2

0

In recent versions of Python, os.popen() is just a wrapper for subprocess.Popen() which merely starts a process.

You probably want

from subprocess import run
with open('test.bin', 'wb') as bin:
    run(["curl", url], stdout=bin, check=True)

subprocess.run() is a higher-level wrapper which takes care of running the subprocess to completion and properly waiting for it to finish, etc. which Popen() and thus os.popen() does not do.

For more details, see also Running Bash commands in Python

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

I ran your code, I believe you have an unnecessary " in your code before the file name and the python reads test.bin as a variable, not a string in os.popen...

os.popen("curl "+url+" > test.bin")

This works for me, I hope it helps :)

makfazlic
  • 33
  • 4