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

删除回忆录丶 提交于 2020-01-17 06:45:28

问题


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.

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 -")

Is there any way to tell python not to interpret # as comment symbol?


回答1:


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 -")

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.



来源:https://stackoverflow.com/questions/42652222/how-to-tell-python-not-to-interpret-hash-symbol-as-comment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!