I was wondering if i could open any kind of application in Python during runtime?
Assuming that you are using Windows you would use one of the following commands like this.
subprocess.call
import subprocess
subprocess.call('C:\\myprogram.exe')
os.startfile
import os
os.startfile('C:\\myprogram.exe')
Using system you can also take advantage of open function (especially if you are using mac os/unix environment. Can be useful when you are facing permission issue.
import os
path = "/Applications/Safari.app"
os.system(f"open {path}")
Try having a look at subprocess.call http://docs.python.org/2/library/subprocess.html#using-the-subprocess-module
Use the this code : -
import subprocess
subprocess.call('drive:\\programe.exe')
Try this :
import os
import subprocess
command = r"C:\Users\Name\Desktop\file_name.exe"
os.system(command)
#subprocess.Popen(command)
Of course you can. Just import import subprocess and invoke subprocess.call('applicaitonName').
For example you want to open VS Code in Ubuntu:
import subprocess
cmd='code';
subprocess.call(cmd)
This line can be also used to open application, if you need to have more information, e.g. as I want to capture error so I used stderr
subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
Some extra examples for Windows, Linux and MacOS:
import subprocess
# Generic: open explicitly via executable path
subprocess.call(('/usr/bin/vim', '/etc/hosts'))
subprocess.call(('/System/Applications/TextEdit.app/Contents/MacOS/TextEdit', '/etc/hosts'))
# Linux: open with default app registered for file
subprocess.call(('xdg-open', '/tmp/myfile.html'))
# Windows: open with whatever app is registered for the given extension
subprocess.call(('start', '/tmp/myfile.html'))
# Mac: open with whatever app is registered for the given extension
subprocess.call(('open', '/tmp/myfile.html'))
# Mac: open via MacOS app name
subprocess.call(('open', '-a', 'TextEdit', '/etc/hosts'))
# Mac: open via MacOS app bundle name
subprocess.call(('open', '-b', 'com.apple.TextEdit', '/etc/hosts'))
If you need to open specifically HTML pages or URLs, then there is the webbrowser module:
import webbrowser
webbrowser.open('file:///tmp/myfile.html')
webbrowser.open('https://yahoo.com')
# force a specific browser
webbrowser.get('firefox').open_new_tab('file:///tmp/myfile.html')
Related
I want to execute another program that I compiled earlier from python using the subprocess.call(command) function.
However, python states that it cannot find the command. I suspect that subprocess is not able to find my custom command since it does not know the PATH variable of my Ubuntu system. Is it possible to somehow execute the following code, where command is part of my PATH?
import subprocess
subprocess.run("command -args")
Running this code leads to the error command not found.
You can either provide the explicit path to your command:
subprocess.run('/full/path/to/command.sh')
or else modify your PATH variable in your Python code:
import os
os.environ['PATH'] += ':'+'/full/path/to/'
subprocess.run('command.sh')
You can modify the environment variables. But be careful when you pass arguments.
Try something like this:
import os
import subprocess
my_env = os.environ.copy()
my_env["PATH"] = "/usr/test/path:" + my_env["PATH"]
subprocess.run(["command", "-args"], env=my_env)
Trying to make it not a hassle to open pyCharm in Kali Linux. I've been following an online class, and the way to open pyCharm is to navigate to the .sh file and open it. I tried automating the process using the subprocess module, but it gives the error:
no such file or directory: 'cd': 'cd'
My code:
import subprocess
subprocess.call("cd")
subprocess.call("cd", "Downloads/pycharm-community-2019.2.3/bin")
subprocess.call("/.pycharm.sh")
set the shell to True:
import subprocess
subprocess.call("cd", shell=True)
subprocess.call("cd", "Downloads/pycharm-community-2019.2.3/bin", shell=True)
subprocess.call("/.pycharm.sh", shell=True)
#it is "./pycharm.sh"
I have made a folder using python3 script, and to apply multiple attributes (+h +s) to the folder I have to run ATTRIB command in Command Prompt.
But I want to know how it can be done from the same python3 script.
import os
os.makedir("C:\\AutoSC")
# Now I want the code to give the same result such that I have opned CMD and writen following command
# C:\> attrib +h +s AutoSC
# Also show in the code, necessary imported modules
I want the folder to be created and immediately hidden as system folder.
Which is not visible even after show hidden files.
Use the subprocess module or use os.system to send commands directly to OS.
import subprocess
subprocess.run(["ls","-l"])# in linux, for windows, it may change.
import os
os.system('attrib +h +s AutoSC')
I have a script in my project's bin directory, and I want to execute it from a cron. Both scripts are written in python.
Target file :
App_directory/bin/script_name
Want to execute script_name script with some parameters from App_directory/cron/script_name1.py
How do I achieve that ?
Try:
import os
os.system('/path/to/App_directory/bin/script_name')
Or if script_name is not executable and/or doesn't have the shabang (#!/usr/bin/env python):
import os
os.system('python /path/to/App_directory/bin/script_name')
The subprocess module is much better than using os.system. Just do:
import subprocess
subprocess.call(['/path/to/App_directory/bin/script_name'])
The subprocess.call function returns the returncode (exit status) of the script.
It works for me...
import subprocess
process = subprocess.Popen('script_name')
print process.communicate()
I've wrote a nice app for myself using the Twisted framework. I launch it using a command like:
twistd -y myapp.py --pidfile=/var/run/myapp.pid --logfile=/var/run/myapp.log
It works great =)
To launch my app I wrote a script with this command because I'm lazy^^
But since I launch my app with the same twistd option, and I tink the script shell solution is ugly, how I can do the same but inside my app? I'd like to launch my app by just doing ./myapp and without a shell work around.
I've tried to search about it in twisted documentation and by reading twisted source but I don't understand it since it's my first app in Python (wonderful language btw!)
Thanks in advance for anyhelp.
You need to import the twistd script as a module from Twisted and invoke it. The simplest solution for this, using your existing command-line, would be to import the sys module to replace the argv command line to look like how you want twistd to run, and then run it.
Here's a simple example script that will take your existing command-line and run it with a Python script instead of a shell script:
#!/usr/bin/python
from twisted.scripts.twistd import run
from sys import argv
argv[1:] = [
'-y', 'myapp.py',
'--pidfile', '/var/run/myapp.pid',
'--logfile', '/var/run/myapp.log'
]
run()
If you want to bundle this up nicely into a package rather than hard-coding paths, you can determine the path to myapp.py by looking at the special __file__ variable set by Python in each module. Adding this to the example looks like so:
#!/usr/bin/python
from twisted.scripts.twistd import run
from my.application import some_module
from os.path import join, dirname
from sys import argv
argv[1:] = [
'-y', join(dirname(some_module.__file__), "myapp.py"),
'--pidfile', '/var/run/myapp.pid',
'--logfile', '/var/run/myapp.log'
]
run()
and you could obviously do similar things to compute appropriate pidfile and logfile paths.
A more comprehensive solution is to write a plugin for twistd. The axiomatic command-line program from the Axiom object-database project serves as a tested, production-worthy example of how to do similar command-line manipulation of twistd to what is described above, but with more comprehensive handling of command-line options, different non-twistd-running utility functionality, and so on.
You can also create the options / config for a twisted command and pass it to the twisted runner.
#!/usr/bin/env python3
import twisted.scripts.twistd
import twisted.scripts._twistd_unix
config = twisted.scripts._twistd_unix.ServerOptions()
config.parseOptions([
"--nodaemon",
"web",
"--listen=tcp:80",
"--path=/some/path"
])
twisted.scripts._twistd_unix.UnixApplicationRunner(config).run()