我想运行 ps -ef | awk '$8=="linuxdcpp"{print $2}'
在使用 os 库的 python 脚本中。
当我尝试将其放入 os.system() 时。我遇到了以下问题:
os.system("ps -ef | awk '$8=="linuxdcpp"{print $2}'")
会引发错误,同样 os.system('ps -ef | awk '$8=="linuxdcpp"{print $2}'')
.
我该如何解决这个错误?
最佳答案
很简单:
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.
Python
简介 strings .
关于linux - Python字符串引号麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33444746/