Hello everybody, I have a question.
There is a console command that I want to convert into python code.
Here's the story.
I compiled a command to turn USB hub on/off by following this page Turning usb ports on and off
Basically what I did is:
Copy the source code of hub-ctrl.c to my Raspberry Pi
Change into the directory where the hub-ctrl.c code was copied
Compile it into an executable named hub-ctrl (note the lack of a .c)
Copy the compiled executable to the root directory
Change back into the root directory
Now I can run the newly compiled hub-ctrl command, by running this code in console:
sudo ./hub-ctrl -h 0 -P 2 -p 0 ; sleep 5; sudo ./hub-ctrl -h 0 -P 2 -p 1;
Which turns off USB hubs for 5 seconds, and restart it. It works fine.
But, I want to run that code in Python.
So, I did this in Python so I can use console commands in Python code:
from subprocess import call
call(["sudo", "./hub-ctrl", "-h", "0", "-P", "2", "-p", "0", ";", "sleep", "5;", "sudo", "./hub-ctrl", "-h", "0", "-P", "2", "-p", "1;"])
But it will not work. It will give an error
sudo: ./hub-ctrl: command not found
How can I run that command from the newly made excutable in Python code?
How should I modify that to make it work?
Any other method?
Thanks in advance. Appreciate any help
Right, so your solution should work, the issue is that your python script doesn't know where the hub-ctrl binary lives. Using the ./hub-ctrl call assumes that your python script is being run from the same directory where your hub-ctrl executable is. Try dumping the hub-ctrl binary where it's going to be in the standard PATH, like /usr/bin
I see.
Can I dump the hub-ctrl into the same directory the Python code file is in instead?
How should I phrase the Python code if I do that?
Remove the sudo from your python code and run the python script with sudo instead
Sorry, you need to Log In to post a reply to this thread.