I would like to write down what I was addicted to as a Python super beginner.
I touched Python for the first time when I touched Raspberry Pi. So I'm a python super beginner. I wrote a program that combines a Raspberry Pi and an ultrasonic sensor to play an audio file when approaching a certain distance. As for the content, the value (distance) actually acquired was written to a log file, and the audio file was played when it became 80 cm or less.
First, manually execute the command in the python file! When I brought my hand close to the sensor, the audio file was executed normally.
The next time I started Raspberry Pi, I modified it to run the python file automatically. (Easy task of adding commands to /etc/rc.local) After starting Raspberry Pi, open the log file, check that the sensor is working, and then bring your hand close to the sensor, that? The audio file does not play ... but the value is 80 cm or less ... What's going on ...? As a beginner in programming, I was a little confused at this point.
As a result of various trials and errors, the cause was that the path to specify the python file to play the audio file was written as a relative path.
·Change before
test.py
if (read_distance()) < 80:
check = subprocess.Popen(['python','music.py'])
print check
・ After change
test.py
if (read_distance()) < 80:
check = subprocess.Popen(['python','/home/pi/music.py'])
print check
Until now, I used to write with relative paths, so I unknowingly wrote with relative paths. If you write with a relative path, the reference directory will change, so it seems safe to write with an absolute path.
I thought I would write such a simple thing so that I wouldn't repeat the same mistake when I forgot. I feel that programming in a new language requires patience ...
Recommended Posts