web-dev-qa-db-ja.com

ファイルが存在するかどうかを確認し、存在しない場合は作成します

私は試してみました:キャッチしましたが、機能していません。 ifステートメントに変更できると思いますが、なぜこれが機能しないのかわかりません。これは私の最初の「本当の」プロジェクトです。私は灌漑コントローラーを構築し、灌漑のスケジュールの辞書を作成しています。最初のコードは私がこれまでに持っているコードであり、2番目のコードは私が試しているそれ自体の「テスト」です。コードを実行するたびに、既存のファイルを書き換えます。必要なのは、ファイルが既に存在する場合はファイルを開き、再度書き込まないことです。

# timer will first look for a saved file(dictionary) of already recorded
# irrigation times.  If no file exists it will create one.  

# irrigation timer which does scheduled irrigation as well as cyclic   irrigation for propagating plants.
# uses a lcd 1602 display
# will use up to 10 different valves

import time
import datetime
import threading
import RPi.GPIO as GPIO
from RPLCD import CharLCD # http://www.circuitbasics.com/raspberry-pi-lcd-set-up-and-programming-in-python/

GPIO.setmode(GPIO.BOARD)

# pinouts for lcd pins
lcd = CharLCD (cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23]) 

# Valve pins
Valve_1 = 8
Valve_2 = 10
Valve_3 = 12
Valve_4 = 16
Valve_5 = 18
Valve_6 = 22
Valve_7 = 24
Valve_8 = 26
Valve_9 = 32
Valve_10 = 36

# setup Valve pins as outputs
GPIO.setup(Valve_pin1, GPIO.OUT)
GPIO.setup(Valve_pin2, GPIO.OUT)
GPIO.setup(Valve_pin3, GPIO.OUT)
GPIO.setup(Valve_pin4, GPIO.OUT)
GPIO.setup(Valve_pin5, GPIO.OUT)
GPIO.setup(Valve_pin6, GPIO.OUT)
GPIO.setup(Valve_pin7, GPIO.OUT)
GPIO.setup(Valve_pin8, GPIO.OUT)
GPIO.setup(Valve_pin9, GPIO.OUT)
GPIO.setup(Valve_pin10, GPIO.OUT)

#set all Valve pins to off
GPIO.output(Valve_pin1, False)
GPIO.output(Valve_pin2, False)
GPIO.output(Valve_pin3, False)
GPIO.output(Valve_pin4, False)
GPIO.output(Valve_pin5, False)
GPIO.output(Valve_pin6, False)
GPIO.output(Valve_pin7, False)
GPIO.output(Valve_pin8, False)
GPIO.output(Valve_pin9, False)
GPIO.output(Valve_pin10, False)

# check to see if a schedule has been saved
def sched_check()
    try:
        file = open("schedule.dat", "r")
        schedule = schedule.read()
        file.close()
    # create a list of schedule dictionaries
    except:
        schedule_list = []
        for schedule_number in range(10):
            schedule = {
                                        "timed" : {
                                                  "watering_days" : [],
                                                  "watering_times" : [],
                                                  "duration" : "timed_duration",
                                                  },
                                        "cyclic" : {
                                                  "time_on" : "seconds_on",
                                                  "time_off" : "seconds_off",
                                                  "blackout_window_start" : "blkout_time_start",
                                                  "blackout_window_stop" : "blkout_time_stop",
                                                   },
                                        }
            schedule_list.append(schedule)
        file = open("schedule.dat", "w")
        file.write(str(schedule_list))
        file.close()

そして、これはそれ自体が問題領域です。

def sched_check():
    try:
        file = open("schedule.dat", "r")
        schedule = schedule.read()
        file.close()
        print("file already exists")
    # create a list of schedule dictionaries
    except:
        schedule_list = []
        for schedule_number in range(10):
            schedule = {
                                        "timed" : {
                                                  "watering_days" : [],
                                                  "watering_times" : [],
                                                  "duration" : "timed_duration",
                                                  },
                                        "cyclic" : {
                                                  "time_on" : "seconds_on",
                                                  "time_off" : "seconds_off",
                                                  "blackout_window_start" : "blkout_time_start",
                                                  "blackout_window_stop" : "blkout_time_stop",
                                                   },
                                        }
            schedule_list.append(schedule)
        file = open("schedule.dat", "w")
        file.write(str(schedule_list))
        file.close()
        print("new file created")

sched_check()
9
Nathan Rigg

os.path.exists("schedule.dat")を使用できます。ブール結果を返します。

os.statを使用する代替ソリューションには、次のものが含まれます。

import os
try:
    os.stat("schedule.dat")
    ... # file exists
except:
    file = open("schedule.dat", "w")
    ...

存在しないファイルをstatしようとすると例外が発生します。

5
cs95