简体   繁体   中英

Python issue with linecache reading a line from an external .txt file

I'm hacking together my first Python project. I'm running the below Python code and having trouble with the linecache.getline. I want it to open the stoarage.txt file. Check if the number is 0. If it is, change it to 1 - play the music and turn on the LED. Then switch it to 2 it doesn't play the music again every time I power off and start up my Raspberry Pi. Any suggestions would be super amazing.

My storage.txt is ending up like this:

0

0

0

I thought it should look like this:

2
0
0

My python code looks like this:

import requests
import pygame
import RPi.GPIO as GPIO 
from BeautifulSoup import BeautifulSoup
import linecache


pygame.mixer.init()
pygame.mixer.music.load('chaching.wav')


GPIO.setmode(GPIO.BCM)
#Im using pin 23 for the led. 
GPIO.setup(21, GPIO.OUT)
#Im using pin 4 as a basic switch in replacement for a motion sensor.
GPIO.setup(4, GPIO.IN)

#Cleans up the number taken from my php file.
Soup = BeautifulSoup

#Subscriber number
sub_num = 0

#goal one value
goal1 = 93  
#goal1 celebration flag pulled from the txt file. Line 1 of the doc. 
goal1cel = linecache.getline('storage.txt', 1)
goal2 = 100
goal2cel = linecache.getline('storage.txt', 2)
goal3 = 150
goal3cel= linecache.getline('storage.txt', 3)

detectDaniel = 0


while True:
    response = requests.get('http://www.bringyourownlaptop.com/php-test')
    html= response.content

    soup = BeautifulSoup(html)
    num = soup.find('section')
    sub_num = int(num.contents[0].strip())

    # figure out if goal have been reached and set the appropriate goal celebration flag
    if sub_num == goal1 and goal1cel == 0:
        goal1cel = 1

    if sub_num == goal2 and goal2cel == 0:
        goal2cel = 1

    if sub_num == goal3 and goal3cel == 0:
        goal3cel = 1

    # This passed the current status of the goal1cel e.g. 0 not done, 1 current, 2 finished. 
    text_file = open("storage.txt", "w")
    #This added it to the text document with slash n used as a line break. Weve also had to change the value to a string instead of a integer. 
    text_file.write(str(goal1cel)+"\n"+str(goal2cel)+"\n"+str(goal3cel))
    #closes the file. 
    text_file.close()


    detectDaniel = 1

    # checks if celebrations need to happen from goal celebration flags
    if goal1cel == 1 and detectDaniel == 1:
        GPIO.output(21, True)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy() == True:
            continue
        print("Goal 1 reached!")
        goal1cel = 2

    if goal2cel == 1 and detectDaniel == 1:
        GPIO.output(21, True)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy() == True:
                        continue
        print("Goal 2 reached!")
        goal2cel = 2

    if goal3cel == 1 and detectDaniel == 1:
        print("Goal 3 reached!")
        goal3cel = 2


    print(sub_num)
    print(detectDaniel)

Your mistake lies in assuming that linecache.getline will simply return an integer. If you run it in an interpreter, it will return the string '0\\n' , which is a string.

After you get the line, you're going to need to find some way to simply get the number you want (if they're only ever going to be 1 character values, you might as well just take the first character and do comparisons to characters, eg if goal1cel == '1' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM