[英]Permission to save a file to disk with python script
我有這個測試代碼,它只是將XML文件保存到文件夾中。 我想安排一個crontab作業使其每分鍾運行一次,但是我只能在使用sudo並鍵入密碼時獲得將XML保存到磁盤的權限。 當我使用crontab計划它時,它會打印字符串,但不會保存文件。 我該如何運作?
#!/usr/bin/python
import sys
import time
from socket import *
from xml.dom.minidom import Document
def main(args):
doc = Document()
with open("/Users/lucasp/Desktop/LogsXML" + time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()) + ".xml", "w") as f:
f.write("doc")
string = "File saved! : LogsXML/" + time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()) + ".xml"
print string
if __name__ == '__main__':
sys.exit (main (sys.argv))
在運行腳本之前,請更正權限:查找運行cuid的uid cron(可能是“ crontab”)。 使用chown(可能是root)將LogsXML
文件的gid更改為該組,並授予組對該文件的寫訪問權限。 例如,
sudo chown lucasp:crontab ~lucasp/Desktop/LogsXML
chmod g+w ~lucasp/Desktop/LogsXML
(如果腳本需要創建新文件,則對目錄進行chown,並使用chown g+wx <directory>
授予寫入和執行訪問chown g+wx <directory>
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.