+4 votes
in Programming Languages by (73.8k points)

I want to execute curl command using python os.system() call. The curl statement is similar to the following which contains both single and double quotations.

curl -s "https://www.youtube.com/watch?v=C_dGqBRGmO8" | awk -F'"' '/itemprop="duration"/ { print $4 }'

 If I declare the following, it will not work.

cmd = "curl -s "https://www.youtube.com/watch?v=C_dGqBRGmO8" | awk -F'"' '/itemprop="duration"/ { print $4 }'"

How can I assign the whole curl statement to the variable 'cmd'?

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

You can use triple quotes to assign the whole curl statement to the variable 'cmd'.

cmd = """curl -s "https://www.youtube.com/watch?v=C_dGqBRGmO8" | awk -F'"' '/itemprop="duration"/ { print $4 }'"""

Here is an example using the above curl statement.
>>> import os
>>> cmd = """curl -s "https://www.youtube.com/watch?v=C_dGqBRGmO8" | awk -F'"' '/itemprop="duration"/ { print $4 }'"""
>>> os.system(cmd)
PT124M56S
0

...