python open applications through your program - python

I want my program to open iTunes during runtime. How do I implement this ?
I looked around for answers but didn't get any concrete complete answers. Till now, all I know is I could use the os module and then call the os.system() function to open iTunes. If this is right, what goes into the brackets ?
I have a Mac OS X machine.

One straightforward way to do this on Mac OS X will be to use the open command:
os.system("open -a iTunes")
There are undoubtedly other ways of doing this (e.g, using the Cocoa/Python bridge), but this is the simplest.

This should be able to help you.
From the article linked above...
import sys, string, os, arcgisscripting
os.chdir( 'c:\\documents and settings\\flow_model' )
os.system( '"C:\\Documents and Settings\\flow_model\\flow.exe"' )

Use subprocess.call() if you want to simply run an executable.
os.system() run the command in a subshell, which generates an unnecessary extra process and slightly different behavior depending on the operating system/console used (for example cmd.exe have different escaping than bash)

Read subprocess, is better than os.system in your case.
Subprocess module: http://docs.python.org/2/library/subprocess.html

Related

Subprocess, Launch script in alternate version of Python

My main script, main.py runs in python3. within it, I want to launch another script in some specified version of python.
import subprocess
pysh="/data/data/org.qpython.qpy/files/bin/qpython-android5.sh"
subprocess.call([pysh,'filetext.py'])
Question:
How can I use subprocess to open filetext.py in python2.x or 3.x interchangeably?
I have tried:
I have tried inputting several different arguments to no avail, such as:
os.system('python -2 -m filetext.txt')
or
subprocess.call(['py -2 filetext.txt'])
or
subprocess.call(['C:/Python27/python.exe filetext.txt'])
Any help would be immensely appreciated.
When I try almost the same thing, it seems to work as follows:
import subprocess
print(subprocess.call(["python2", "-c", "import sys; print sys.version"]))
When called from python3 this prints 2.7.5. This will depend of course on if the version of python you want to use is on the PATH, and if not, calling the binary with the full path.
Not sure if it's just a typo here, but I notice you said you wanted to run filetext.py, but you're passing filetext.txt in your examples.
If this doesn't work, I'd have to know more -- you say it doesn't work, but what exactly happens?
Try this :
subprocess.call(['C:/Python27/python.exe', "filetext.txt"])
First you give the path to the executable you need, then the arguments in a different parameter.

How to use python to write a linux shell command?

I want to use python to write a shell command... that I can customize some paths in my config file, that I can "shortcut" to these paths directly, not need to "cd" and "cd" again and again...
I want to use python, because I don't know anything about bash. Is that possible ?if yes, could you give me any idea about how to use python to fullfill this shell command...
Thanks !
You have to give a look to the os library of python https://docs.python.org/2/library/os.html
The os.path module helps you to deal with paths. https://docs.python.org/2/library/os.path.html#module-os.path
With the subprocess module you can also execute commands, configure timouts (if the command hangs out), check the result, etc https://docs.python.org/2/library/subprocess.html

Running command lines within your Python script

So I have a bunch of aliases and Command Line prompt programs, and my main program works by inputting b into the cmd.exe, followed by some filepath names and what not. How would I run those arguments in my python script? So that it mimics the action i am doing in the cmd?
You should use the subprocess module. In particular, subprocess.call will run command line programs for you.
or you can use
import os
os.system('your_command')
for example:
import os
os.system('notepad')
will launch the notepad with the command line behind.
hope this helps
You can do this using subprocess
For example, this call bellow gets the output of the program and stores it as a string, using .call will help with calling it and for more accurate control use .Popen
subprocess.check_output(["ipconfig"])
Check out Sarge - a wrapper for subprocess which aims to make life easier for anyone who needs to interact with external applications from their Python code. and Plumbum - a small yet feature-rich library for shell script-like programs in Python.

Should i continue clearing the screen with the os module or use subprocess?

So, my question is whether i should use the os.system('cls') for clearing command line output in a python command line program. I usually use the os module way, however, i have read that using the subprocess modules are a better choice for doing calls to the command line. In general, which should i use? And if i do use the subprocess way, how would i go about doing it, as i have very little experience with the module, even though i have tried reading the doc's. Thank you in advance for your replies.
If you want to use the subprocess module to invoke cls instead of os.system(), just:
import subprocess
subprocess.call("cls", shell=True)
If you want something more complex see the subprocess documentation.

Python making cmd invisible when using the os.system

I found out that i could ping a system on python by typing
os.system('ping ip')
but when i execute it, it shows cmd.
My question is, how do i ping someone on python without showing the cmd?
If you need only a ping, then it would be better to use something like ping.py.
In other cases use subprocess as suggested by #Sentinel
Look at the
http://docs.python.org/library/subprocess.html
module.
It gives you enough options for controlling the output.
Or use standard bash redirection in order to send the output to /dev/null
See my answer to hiding console when run in os.startfile()

Categories