I have this python file in the same folder as the one being executed. And I'm trying to execute the file in a new terminal window (I'm using Linux) using subprocess. The new window opens, but its giving me the error Failed to execute child process "/usr/bin/python3 /media/root/MYDRIVE/tools/sniff/sniffer.py" (No such file or directory). Could anyone explain?
import subprocess
current_path = os.path.dirname(os.path.realpath(__file__))
subprocess.call(['gnome-terminal', '-x', '/usr/bin/python3 '+ current_path+'/sniffer.py'])
Try the following
import os
os.system("gnome-terminal -e 'bash -c \"python3 sniffer.py\"'")
Related
I am trying to do a script that is hosting my file on my local network, here's my code :
import os
import getpass
os.system('python -m http.server --directory C:/Users/'+getpass.getuser())
But the probleme is that the http console is showing on my Desktop and that's annoying ! so I tried to hide by renaming the file in .pyw but it's not working.
Have you any idea on how to hide this console ? Thank you :D
On Linux you can use Nohup to ignore the HUP signal.
You could add nohup on your code like this:
import os
import getpass
os.system('nohup python -m http.server --directory C:/Users/'+getpass.getuser())
Update
Solution for windows
import os
import subprocess
import getpass
env = os.environ
directory = 'C:/Users/'+getpass.getuser()
proc = subprocess.Popen(['python', '-m', 'http.server', '--directory', directory], env=env)
Assuming you're on Linux (or other unix based OS), you can detach the process from the console after starting the server.
Here is one way to do it with screen command
sudo apt install -Y screen
And then
screen -d -m "python3 script.py"
Where script.py is the snippet you have shared.
Reference for the flags
...
-d -m
Start screen in detached mode. This creates a new session but doesn’t attach to it. This is useful for system startup scripts.
I found a way to do it, with a VBS script:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "main.py" & Chr(34), 0
Set WshShell = Nothing
Just replace main.py with the path of your script.
I want to execute commands in the terminal through a python scripts.
i want to create a script which takes data from a .txt file adds that in a list and then one by one execute them in the terminal.
what i am looking for is a process to execute commands in the terminal in Kali Linux, I couldn't find anything online.
like in windows we use import subprocess or import os
Thank you.
example command is like
python3 app.py
Try this:
import subprocess
command = "python3 app.py"
subprocess.call(command, shell=True)
You can use the os.system function. It returns the return value of the command run.
E.g.,
status = os.system('echo hello')
I'm trying to write a script that opens a new terminal then runs a separate python script from that terminal.
I've tried:
os.system("gnome-terminal 'python f.py'")
and
p = Popen("/usr/bin/gnome-terminal", stdin=PIPE)
p.communicate("python f.py")
but both methods only open a new terminal and do not run f.py. How would I go about opening the terminal AND running a separate script?
Edit:
I would like to open a new terminal window because f.py is a simply server that is running serve_forever(). I'd like the original terminal window to stay "free" to run other commands.
Like most terminals, gnome terminal needs options to execute commands:
gnome-terminal [-e, --command=STRING] [-x, --execute]
You probably need to add -x option:
x, --execute
Execute the remainder of the command line inside the terminal.
so:
os.system("gnome-terminal -x python f.py")
That would not run your process in the background unless you add & to your command line BTW.
The communicate attempt would need a newline for your input but should work too, but complex processes like terminals don't "like" being redirected. It seems like using an interactive tool backwards.
And again, that would block until termination. What could work would be to use p.stdin.write("python f.py\n") to give control to the python script. But in that case it's unlikely to work.
So it seems that you don't even need python do to what you want. You just need to run
python f.py &
in a shell.
As of GNOME Terminal 3.24.2 Using VTE version 0.48.4 +GNUTLS -PCRE2
Option “-x” is deprecated and might be removed in a later version of gnome-terminal.
Use “-- ” to terminate the options and put the command line to execute after it.
Thus the preferred syntax appears to be
gnome-terminal -- echo hello
rather than
gnome-terminal -x echo hello
Here is a complete example of how you would call a executable python file with subprocess.call Using argparse to properly parse the input.
the target process will print your given input.
Your python file to be called:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--file", help="Just A test", dest='myfile')
args = parser.parse_args()
print args.myfile
Your calling python file:
from subprocess import call
#call(["python","/users/dev/python/sandboxArgParse.py", "--file", "abcd.txt"])
call(["gnome-terminal", "-e", "python /users/dev/python/sandboxArgParse.py --file abcd.txt"])
Just for information:
You probably don't need python calling another python script to run a terminal window with a process, but could do as follows:
gnome-terminal -e "python /yourfile.py -f yourTestfile.txt"
The following code will open a new terminal and execute the process:
process = subprocess.Popen(
"sudo gnome-terminal -x python f.py",
stdout=subprocess.PIPE,
stderr=None,
shell=True
)
I am running a uWS server with this.In my case Popen didn't help(Even though it run the executable, still it couldn't communicate with a client -: socket connection is broken).This is working.Also now they recommends to use "--" instead of "-e".
subprocess.call(['gnome-terminal', "--", "python3", "server_deployment.py"])
#server_deployment.py
def run():
execution_cmd = "./my_executable arg1 arg2 dll_1 dll_2"
os.system(execution_cmd)
run()
I am new to windows python. I am trying to run a command line tool using python. This tool will flash the firmware connecting to IP address of the machine. I could open cmd prompt and use the command
C:\ToolsSuite>sdi --ip 172.23.240.41 --fwdl "c:\BUILDS\firmware_image.zip
.This works for me very well.
But when I try to execute using the python script on windows, I am not able to do that. Python script looks like this.
import subprocess
import os
os.chdir(r"C:\ToolsSuite")
#os.system('cd c:\mydir')
os.system("sdi --ip 192.92.48.32 --fwdl C:\firmware_image.zip")
#subprocess.Popen(r'sdi --ip 192.92.48.32 --fwdl "c:\firmware_image.zip"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
The exception thrown is "Could not find file". I am not getting how to give the path of the firmware file when it is stored in some location, say for example 'C' drive or in some folder location of windows.
If the sdi executable is in "C:\ToolsSuite", this should work:
subprocess.call(['sdi', '--ip 192.92.48.32', r'--fwdl "c:\firmware_image.zip"'])
If you want to call a Windows command, you need to give the full path to the command.
You can try:
import subprocess
import os.path
# C:\ToolsSuite>sdi --ip 172.23.240.41 --fwdl "c:\BUILDS\firmware_image.zip"
cmd = os.path.join("C:\\ToolsSuite", "sdi")
args = [cmd,
'--ip', '172.23.240.41',
'--fwdl', 'c:\\BUILDS\\firmware_image.zip']
subprocess.check_call(args)
Here, check_call is useful to replace non-zero exit code by an exception. Of course, you can also choose another function of the same family.
I am using py2app to bundle a python script, that uses the anaconda python distribution.
Since py2app doesn't play well with terminal scripts that need user input, I have a Tkinter file that py2app's setup.py launches, which then further launches my .py script using popen.
Locally, this works fine:
import subprocess as sub
command = "~/anaconda/bin/python -u myscript.py " + str(groups_count)
process = sub.Popen(command, shell=True, stdout=sub.PIPE, stderr=sub.PIPE, bufsize=1, universal_newlines=True)
But when I want to distribute this, I need to replace hardcoded paths and run this using the distribution contained within
import subprocess as sub
command = sys.executable + " -u myscript.py " + str(groups_count)
process = sub.Popen(command, shell=True, stdout=sub.PIPE, stderr=sub.PIPE, bufsize=1, universal_newlines=True)
This results in an error:
/bin/sh: /Users/username/projectname/appname/dist/MyOSXapp.app/Contents/MacOS/python: Permission denied
If I look at libpython2.7.dylib within /MyOSXapp.app/Contents/Frameworks it doesn't seem to be executable, but is readable by everyone. This is all assembled by py2app.
I need to run popen on my anaconda python distributed within pyapp. How do I do this?
I worked around this by adding executable permission to the python file saved by py2app within the osx .app file.
$ chmod +x ./dist/MyOSXapp.app/Contents/MacOS/python
Additionally, a fix for this was also added in py2app via issue 228.