如何告诉 python 不要将哈希符号解释为注释?

How to tell python not to interpret hash symbol as comment?

我想通过 python 中编写的 os.system 命令打开和关闭 crone 作业。基本上,使用 sed 命令 comment/uncomment crontab 行来控制作业计划。

但是当我如下所示输入此命令时,python 解释为 # 之后的文本作为注释。

import os
os.system("crontab -l | sed '/^\*.*heightSQL.py/s/^/#/' | crontab -")

有什么方法可以告诉 python 不要将 # 解释为注释符号吗?

您对哈希符号没有问题,但您的引用不正确。试试这个:

os.system(r"crontab -l | sed '/^\*.*heightSQL.py/s/^/#/' | crontab -")

顺便说一句:您确定 crontab -l 产生正确的输出而 crontab - 已经是 运行 了吗?我不是。但这不是重点。