Opening a program through the terminal in Kali Linux - python

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"

Related

Pyinstaller -w exe still opening terminals in Windows

I ran this command to convert my script from .py to .exe:
pyinstaller.exe -w -i .\icon.png --add-data='icon.png;.' .\gui_script.py
and after adding in some poorly added packages the GUI opens via my tkinter script. But when I run code using the tkinter window in Windows, every time there's a subprocess.run or os.system function it opens up a new terminal window. Is there any way to suppress these? Or at least make them minimized or not noticeable?
Here's a piece of the gui_script.py that combines two files which opens an external terminal window.
import os
os.system('copy cDNA.fa+ncRNA.fa transcriptome.fa /b')
I started using python tools for the merging of files:
with open('transcriptome.fa','wb') as transcriptome_file:
for fasta_file in ['cDNA.fa','ncRNA.fa']:
with open(fasta_file,'rb') as current_fasta:
shutil.copyfileobj(current_fasta, transcriptome_file)
as well as downloading of larger files:
with requests.get('http://ftp.ensembl.org/pub/current_fasta/'+species+'/cdna/'+cDNA_file_name, stream=True) as cDNA_request:
with open('cDNA.fa.gz', 'wb') as cDNA_gz_file:
shutil.copyfileobj(cDNA_request.raw, cDNA_gz_file)
Despite this I still need to run an external program, blast, so there I used subprocess.run with the creationflag = subprocess.CREATE_NO_WINDOW argument like this:
if os.name == 'nt':
blast_db_run = subprocess.run(['makeblastdb',
'-in', fasta_file,
'-dbtype', 'nucl',
'-out','blast_db'],
capture_output=True,
creationflags = subprocess.CREATE_NO_WINDOW)
Used the if statement since creationflags doesn't work in a non-windows environment apparently.

How can I make python open a cmd and set : cd and then command that I need to use

I want to make script that will open and pre set cmd with scrcpy commands.
I tried modules like os,subprocess,pyautogui
When I tried to open cmd with os and type inside it with pyautogui it didnt work
What should I use to type commands.
All I need to write is 'cd "directory"' and scrcpy but I cant find a way to do it with python
You can use following code:
import os
os.system('cmd /c "Your Command Prompt Command"')
This is the code to do it
import os as os
os.startfile("cmd.exe")
And to run the command use
os.system("Your Command")
This will show the command you passed into the cmd terminal.

How to open a python file with cmd

Hi have been working on a project and I have been trying to open a python file with python. So far I have learnt that it is not possible but I have learned that you can do it with cmd. I have got code that can open cmd but I can't seem to work out how to get cmd to open a python file, also if that is possible I would also like to close cmd when the python file is opened. This is what I have so far:
import os
os.system('cmd /k "Python"')
import os
os.system('cmd /k "python filename.py"') # just add your filename
You need to provide your filename to run the python file same as you do when you need to run a python file in cmd/terminal.
If you need to open the python file instead of run that file you can do:
import os
os.system('cmd /k "start notepad++ filename.py"') # open file with notepad++
or
os.system('cmd /k "more filename.py"') # to view file content in cmd
to open file with sublime:
os.system('cmd /k "subl.exe filename.py"') # make sure you have store sublime path in environment variable.
To open a file refer to the documentation at https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
But your intention is not clear. If you want to read it and evaluate it, then you might want to reconsider your approach (but it is possible).
If you want to run code dynamically checkout exec
If you want to start a sub-process in the OS then checkout
https://docs.python.org/3/library/subprocess.html

Popen giving error in python when trying to execute a script

Have having issues with Popen in python. Code in question:
from subprocess import Popen
Popen(["nohup", "/usr/local/bin/python2.7 /somescript.py"])
With following error:
failed to run command `/usr/local/bin/python2.7 /somescript.py': No such file or directory
Thing is that when I run the same command in a terminal, it works and the file definitely exists.
You are missing 2 " and a , Popen takes a list of arguments. Try this:
from subprocess import Popen
Popen(["nohup", "/usr/local/bin/python2.7", "/somescript.py"])

Can I open an application from a script during runtime?

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')

Categories

Resources