What I was addicted to Python autorun

I would like to write down what I was addicted to as a Python super beginner.

I got stuck in an absolute path and a relative path ...

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

What I was addicted to Python autorun
What I was addicted to when using Python tornado
What I was addicted to when migrating Processing users to Python
What I was addicted to when introducing ALE to Vim for Python
What I was addicted to with json.dumps in Python base64 encoding
I was addicted to multiprocessing + psycopg2
[Fixed] I was addicted to alphanumeric judgment of Python strings
I was addicted to pip install mysqlclient
I was addicted to Flask on dotCloud
What I did to save Python memory
I was addicted to confusing class variables and instance variables in Python
[Python] What I did to do Unit Test
[Introduction to json] No, I was addicted to it. .. .. ♬
I was able to recurse in Python: lambda
What I did when updating from Python 2.6 to 2.7
A story I was addicted to when inserting from Python to a PostgreSQL table
I was addicted to creating a Python venv environment with VS Code
Use Python from Java with Jython. I was also addicted to it.
The file name was bad in Python and I was addicted to import
[Python] I was addicted to not saving internal variables of lambda expressions
What I was addicted to when creating a web application in a windows environment
A story that I was addicted to at np.where
A note I was addicted to when running Python with Visual Studio Code
What I learned in Python
I was able to repeat it in Python: lambda
I was addicted to trying logging.getLogger in Flask 1.1.x
A story that I was addicted to when I made SFTP communication with python
I was soberly addicted to calling awscli from a Python 2.7 script registered in crontab
What I was addicted to when combining class inheritance and Joint Table Inheritance in SQLAlchemy
[IOS] GIF animation with Pythonista3. I was addicted to it.
What was surprising about Python classes
What to do with PYTHON release?
I'm addicted to Python 2D lists
I want to debug with Python
What I was addicted to when dealing with huge files in a Linux 32bit environment
A story that I was addicted to calling Lambda from AWS Lambda.
The record I was addicted to when putting MeCab on Heroku
A note I was addicted to when making a beep on Linux
Note that I was addicted to sklearn's missing value interpolation (Imputer)
A note I was addicted to when creating a table with SQLAlchemy
Python> Comprehension> cells> I was taught how to use double comprehension / itertools
What I did when I wanted to make Python faster -Numba edition-
I was a little addicted to installing Python3.3 + mod_wsgi3.4 on Sakura VPS (CentOS), so a retrospective memo
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to implement permutation in Python
I made Othello to teach Python3 to children (4)
What I referred to when studying tkinter
I installed Python 3.5.1 to study machine learning
I made Othello to teach Python3 to children (2)
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I want to use jar from python
I wanted to solve ABC160 with Python
I want to build a Python environment
I want to analyze logs with Python
I want to play with aws with python
I made Othello to teach Python3 to children (5)
I tried to implement ADALINE in Python