+4 votes
in Programming Languages by (14.6k 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

0 votes
by (25.6k points)

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
...