Laschet Jain
Laschet Jain

Reputation: 744

Python string quotes trouble

I want to run ps -ef | awk '$8=="linuxdcpp" {print $2}' in a python script using os library.

When I try to put this in os.system(). I run into following trouble:

os.system("ps -ef | awk '$8=="linuxdcpp" {print $2}'") will raise an error, and similarly os.system('ps -ef | awk '$8=="linuxdcpp" {print $2}'').

How can I resolve this error?

Upvotes: 1

Views: 177

Answers (1)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

It's quite simple:

os.system("""ps -ef | awk '$8=="linuxdcpp" {print $2}'""")

String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line.

Introduction to Python strings.

Upvotes: 3

Related Questions