如何告诉python不要将哈希符号解释为注释? [英] How to tell python not to interpret hash symbol as comment?
问题描述
我想通过python编写的os.system命令打开和关闭crone作业。基本上,使用sed命令注释/取消注释crontab行以控制作业计划。
I wanted to turn on and turn off crone job from os.system command written in python. Basically, using sed command to comment/uncomment the crontab line to control the job schedule.
但是当我按如下所示放置此命令时,python解释为#后的文本为
But when I put this command as shown below, python interpret as text after # as comment.
import os
os.system("crontab -l | sed '/^\*.*heightSQL.py/s/^/#/' | crontab -")
有什么方法可以告诉python不要将#解释为注释符号?
Is there any way to tell python not to interpret # as comment symbol?
推荐答案
您的哈希符号没有问题,但是引用不正确。试试这个:
You have no issue with the hash symbol but your quoting is not proper. Try this:
os.system(r"crontab -l | sed '/^\*.*heightSQL.py/s/^/#/' | crontab -")
还有btw:您确定 crontab -l </ code>在
crontab-
已经运行时会产生正确的输出吗?我不是。但这不在这里。
And btw: Are you sure crontab -l
produces proper output while crontab -
is already running? I'm not. But that's beside the point here.
这篇关于如何告诉python不要将哈希符号解释为注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!