Question
How can I change the active directory on the raspberry pi using cd and the subprocess module?
Background
Since I absolutely hate to use the command line, I am trying to create a basic GUI text-editor which can also compile my programs. For now, I am just trying to change the directory to Desktop. To do this, I am using the subprocess module. Here is my current code:
from subprocess import *
call(["cd","Desktop"])
In the terminal, this line (cd Desktop) would change the active directory to Desktop. Oddly, when I run it through subprocess, I am given this error:
OSError: [Errno 2] No such file or directory
Tech Specs
Raspberry Pi Model B
Raspbian "Wheezy" OS
I would try os.chdir
import os
os.chdir("/path/to/dir")
i don't mean to derail the original question, but if you're trying to automate a lot of tasks, you can use the fabric module.
it has a rather simple syntax like this:
with cd('/path/to/app'):
with prefix('workon myvenv'):
run('./manage.py syncdb')
http://docs.fabfile.org/en/1.6/api/core/context_managers.html
it's designed for remote usage over ssh, but many people use it for a lot of local management & deployment
the lcd command works on your local machine:
with lcd('/path/to/app'):
with prefix('workon myvenv'):
run('./manage.py syncdb')
Related
So for a little context. In linux the "ifconfig" command is actually executing a "ifcfg-eth0" file found "/etc/sysconfig/network-scripts."
In windows, do command line (or powershell) commands correspond to a specific file? If so where? If it exists I having a hard time finding it.
Reason:
I am trying to execute commands from a program I am writing in Python. I know there are other ways to accomplish this ie. "import os, import subproccess." I am trying to brainstorm a simpler way to execute these commands before my program gets to heavy.
Basically I would like to tell python to execute a file ie. "ifcfg-eth0" in linux but in windows. Also, I'm just using "ipconfig" as an example There are a lot of commands I want to add.
In Windows, an easy way to find the path of a program is to use Where.exe.
where.exe ipconfig
Usually cmd commands are located in system32.
There's an executable ipconfig.exe in C:\Windows\system32
I have developed a Python application which needs to call a bash script stored in another computer (Raspberry Pi).
I don't need to get any return value nor confirmation.
What are the feasible ways to do that?
Thanks!
From the shell you could do it like this:
ssh pi#theraspberrypi "./myscript"
To run a shell command from in Python:
import os
os.system("ssh pi#theraspberrypi ./myscript")
Or, as Eevee suggested below:
import subprocess
subprocess.call(['ssh pi#theraspberrypi ./myscript'], shell=True)
Of course, you will probably want to put your public key in the raspberry pi's authorized_keys file so it won't prompt for a password.
So I am trying to do following:
I have Cygwin enabled with screen and ssh daemon in Windows 7.
I create a new screen using the command screen -dmS "my_screen" on my Windows machine.
I ssh to the Windows machine from my Linux machine.
I attach to it from my unix machine using screen -d -r my_screen
Now I try to launch a Windows application, for example notepad.exe.
Now I want to a automate this using Python. The objective is to just manually ssh to Windows and then run a Python script which will do the above steps. I have written the following script but it is not working:
import shlex
import os
import time
import subprocess
cmdString = "screen -d -r default_screen"
cmdArgs=shlex.split(cmdString)
p=subprocess.Popen(cmdArgs)
cmds = "./notepad.exe"
cArgs=shlex.split(cmds)
pp=subprocess.Popen(cArgs)
This is not working. :( Basically to get the screen I will probably need to import pty package or tty. But pty & tty are not supported in Windows. I am able to attach to the newly created screen but then attempt to launch the Windows program like notepad for example fails. It hangs and the windows GUI is not launched as it would when down manually.
I am still exploring this but I will appreciate it if someone can point me to the right way to do it.
I put the screen command in the bash profile script of cygwin user. This is working now.
I have written a program in python that is another front end for un-tarring files on OSX. I want to be able to double click on the file and open with the program that I wrote. How do I make the program automatically know that it is being opened with that file, and to work on that file?
You will need to make your make your script executable with the command chmod a+x <your script.py>
You will also need to tell your OS that the python interpreter is needed to execute this file. In Linux the line #!/usr/bin/env python at the top of the file is what does this. I assume its the same in OSX.
Then right click and select "Open with" and then "Other...". And select your script.
The Python script will need to be configured correctly to run when the OS calls it in this way.
Another way to do it(Caveat: I haven't personally tried this) is to use a shell script as advised in this anwer on the superuser forums.
Python scripts on OS X are not by default recognized as launchable applications by the Finder. What you need to do is use a tool that will package your Python script as a Mac OS X application. Then, you can right-click or ctrl-click a file of your chosen file type and locate that application through the Open With... dialog.
One option I'm aware of for doing this is part of the MacPython package, called py2app:
http://wiki.python.org/moin/MacPython/py2app
So, your steps would be:
1) Use py2app to package your Python script as a launchable application
2) Associate the application with the file type you'd like it to open using by right-clicking or ctrl-clicking one of that file type and choosing "Open With..." in the OS X Finder.
import sys
print("Program.py executing...")
for x in sys.argv:
print(x)
Produces the output
Program.py executing...
Program.py
something.tar
I'm windows, running this command:
python Program.py something.tar
I don't think anything changes from that command to OSX.
And as said by benjamin, the OS is responsible for determining what program is used for the file. I don't know what the command line from your os will be, but you can find out with the above program.
In a Unix system, within a python script, I am trying to open a terminal window and start a server. It is my understanding that python has a subprocess module that is supposed to allow such a thing. So:
import subprocess
subprocess.Popen(['path to terminal'])
returns:
OSError: [Errno 13] Permission denied
How do I run this with the right permissions? Or, is there a better, secure way to do what I need?
I'm relatively new to programming, so please reorient the discussion if my question is misguided. Thank you!
Edit: you state that you would like to execute /Applications/Utilities/Terminal.app, so you are apparently running Mac OS X.
Mac OS X .app programs are directories. They can be started with the Mac OS shell command open.
To open the program /path/to/server in a fresh Max OS Terminal session:
import subprocess
termapp=['open','-a','/Applications/Utilities/Terminal.app']
sp=subprocess.Popen(termapp+['/path/to/server'])
There's also a shell-command version of the terminal, so you do not need open -a.
import subprocess
termapp=['/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal']
sp=subprocess.Popen(termapp+['/path/to/server'])
The two ways have subtle differences in how the windows are grouped by the window manager. Each time you do the above you get another terminal process and another icon in the tray. While with -a a new window is opened within the same Terminal main program.