亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 腳本不會在啟動時啟動

Python 腳本不會在啟動時啟動

江戶川亂折騰 2023-06-20 16:19:19
我的 Raspberry Pi 中有一個連接到雨量計的 python 腳本。當雨量計檢測到下雨時,腳本顯示 0.2 并將其寫入文件。這是代碼:#!/usr/bin/env python3import timeimport RPi.GPIO as GPIOBUTTON_GPIO = 16if __name__ == '__main__':    GPIO.setmode(GPIO.BCM)    GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)    pressed = False    while True:        # button is pressed when pin is LOW        if not GPIO.input(BUTTON_GPIO):            if not pressed:                print("0.2")                pressed = True        # button not pressed (or released)        else:            pressed = False        time.sleep(0.1)我的想法是使用這樣的代碼來保存總降雨量。當 python 腳本顯示 0.2 > 將其寫入文件時。python3 rain.py >> rain.txt代碼創建一個文件,但在 Ctrl + C 完成執行之前不寫入任何內容。我需要在啟動時執行它。我試圖將它添加到 crontab 和 rc.local 但不起作用。我試著用 sudo 和 pi 來執行它。權限為755。
查看完整描述

2 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

嘗試這個


import time

import RPi.GPIO as GPIO

BUTTON_GPIO = 16


if __name__ == '__main__':

    outputfile=open("/var/log/rain.txt","a",0)

    GPIO.setmode(GPIO.BCM)

    GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    pressed = False

    while True:

        # button is pressed when pin is LOW

        if not GPIO.input(BUTTON_GPIO):

            if not pressed:

                openputfile.write("0.2\n")

                pressed = True

        # button not pressed (or released)

        else:

            pressed = False

        time.sleep(0.1)

以非緩沖寫入的追加模式打開文件。然后當事件發生時,寫入該文件。


不要使用 shell 重定向,因為它(在這種情況下)會緩沖所有程序輸出,直到退出,然后寫入文件。當然,退出永遠不會發生,因為你有一個沒有中斷的“while True”


查看完整回答
反對 回復 2023-06-20
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

實際上,此構造command >> file將整個stdout并沖入文件。它僅在command執行結束時完成。您必須在中間結果準備就緒后立即寫入文件:


#!/usr/bin/env python3

import sys

import time

import RPi.GPIO as GPIO

BUTTON_GPIO = 16


if __name__ == '__main__':

    GPIO.setmode(GPIO.BCM)

    GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    pressed = False


    # command line arguments

    if len(sys.argv) > 1: ## file name was passed

        fname = sys.argv[1]

    else: ## standard output

        fname = None


    ## this will clear the file with name `fname`

    ## exchange 'w' for 'a' to keep older data into it

    outfile = open(fname, 'w')

    outfile.close()


    try:

        while True:

            # button is pressed when pin is LOW

            if not GPIO.input(BUTTON_GPIO):

                if not pressed:

                    if fname is None: ## default print

                        print("0.2")

                    else:

                        outfile = open(fname, 'a')

                        print("0.2", file=outfile)

                        outfile.close()

                    pressed = True

            # button not pressed (or released)

            else:

                pressed = False

            time.sleep(0.1)

    except (Exception, KeyboardInterrupt):

        outfile.close()

在這種方法中,你應該運行python3 rain.py rain.txt,一切都會好起來的。該try except模式確保當執行被錯誤或鍵盤事件中斷時文件將被正確關閉。


注意file調用中的關鍵字參數print。它選擇一個打開的文件對象來寫入打印的東西。它默認為sys.stdout.


查看完整回答
反對 回復 2023-06-20
  • 2 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號