问题
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