linux - Python字符串引号麻烦

标签 linux python-2.7

我想运行 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/

相关文章:

python-2.7 - 如何为 Exact Target API 配置 FUEL SDK 环境变量?

python - 如果存在特定单词,则将行值替换为 NaN - Python

Linux 命令行使用 for 循环并格式化结果

python - 为什么 Python 升级到 3.8 后 Pip 无法运行?

linux - 如何从bash中的两个模式之间提取字符串

python - 在 python 中使用 lambda 函数创建差异数组

python - Python中的for循环造句

python - 如何查找和卸载 numpy 重复版本

linux - 在 bash 脚本中调用 SSH Sudo 命令

linux - 删除不在 repo 中的 git LFS 文件