I am trying to trying to control the GPIOs on my RPi with Python but without one of those modules that require root permissions to be used. I found that trivial way of just "echoing" 1 or 0 to the value file of the GPIO through the console. I also wrote a Python script doing this automatically. So far so good. But this only works if the file (in this case: /sys/class/gpio/gpio17) already exists. I could easily create this folder everytime after booting the Pi, through the console like this:
pi@raspberrypi ~ $ cd /sys/class/gpio
pi@raspberrypi /sys/class/gpio $ echo "17" > export
After this I will have the folder /sys/class/gpio/gpio17 in which I find the information about the pin and can modify it.
So basically my question is: How would I incorporate this step into my Python script. Is there a way to move to the directory and export this folder using Python? I figured it out using a seperate Bash script whitch I then run inside my Python script, but I'd like to avoid using several languages and was wondering if there was an equivalent to the "echo" comand in Python. I searched for some days now but couldn't find anything. If there is, a link would be highly appreciated as well!
Thank You in advance,
a newbie
cddoesn't create a directory. It just changes the working directory of the shell to that directory. That won't work if the directory doesn't exist. You also don't need to be in the directory to echo to a file in the directory (unless something odd is going on here).RPi.GPIOAPI that was written for this purpose rather than the sysfs?/sys/class/gpio/exportis not a regular file. Writing17to it will expose pin 17 through the sysfs.