I've got a strange situation, that makes me feel I went wrong at some point.
Environment: I'm running powershell .ps1 script (tried both: powershell and ISE) on Windows 10. In this script I'm runing python (2.7.14) script.
The issue is that I cannot see any output from this python script. This troubles me, because I cannot see the password prompt (https://docs.python.org/2/library/getpass.html used inside) and cannot enter the password.
Tried at least following script invocation methods ($python="C:\Python27_x86\python.exe"):
. $python ...
& "$python ..."
Start-Process $python ...
cmd /c "$python ..." (as suggested in PowerShell is Buffering Python Stdout)
Any ideas, what is the reason for such behavior and how can I pass the password to the script?
There must be a better way to do this than e.g.: Powershell: Capture program stdout and stderr to separate variables
Related
I'm using Python to execute some bash commands. The problem is that the terminal outputs from these bash scripts are spamming my terminal. Is there any way to block the output messages from these scripts? I have tried the step in this answer. But is only blocking the print calls I make, and it is not blocking the console outputs from the bash commands.
Can anyone suggest any better solution?
In Bash you can simply use:
$ eclipse &>/dev/null
This catches both stdin and stderr to the redirect point (in bash).
(here eclipse is my command like)
I would like to include a command to create a 7zip archive withinin a Python script. Since I am working on Windows, I need to pass the command to the powershell console. I am planning to do it with os.system (I am aware that this is not the best way to do it and that I should use subprocess, but I really just need a quick fix and it would not be time effective for me to learn to use a new module in this context).
The following command works if run from the powershell console
&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch
So I recreate the same string within python like this:
cmdl = r"&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch"
The string is interpreted as follow:
"&'C:\\\\Program Files\\\\7-Zip\\\\7z' a -mx=0 X:/myarch.zip X:/myarch"
Now, if I copy-paste the above string within the powershell console, it runs without problems. However, if I run it within python using os.system(cmdl) I got the following error
"The filename, directory name, or volume label syntax is incorrect"
Why is this the case and how can I fix this issue ?
os.system is meant for executing cmd commands, cmd commands can be ran in powershell maybe after all powershell is a bit advanced but I'm sure that you can't run a cmd command in powershell, henceforth your code is not working.
However a creative solution for executing a powershell command from python(not using python) would be to write your command into a .ps file(powershell script)and then run it using os.startfile()(use this code: os.startfile("script.ps"))
I want to initialise a powershell subprocess and be able to send commands to it and receive its output using Python on Windows.
Please can someone help with simple examples? I have gone through almost every other post similar to this on here but haven't had any luck with my specific problem...
I am to run this code to automate a current manual process conducted on Powershell.
This is a high level look at what I want to achieve using Python
I want to launch a powershell session using Python
Then CD into a directory
Run a powershell script in that directory
Display/retrieve all outputs
And pass new commands in the same session, again, whilst capturing outputs
So far all I have is the following code which launches powershell and runs a .ps1 file by giving directory. I'm not sure how to proceed from here or if this is even the best approach given the steps I need to undertake shown above.
p = subprocess.Popen(["powershell.exe", Powershell.exe -executionpolicy remotesigned -File 'G:\My Documents\helloworld.ps1';"]. stdout=sys.stdout)
p.communicate()
After running the above code, python terminal displays the message 'Test Message' correctly
I have a very simple bash script test.sh as shown below
#!/usr/bin/env bash
mkdir "/c/AAA"
I want to execute this code in python. When I call os.system(r"Y:\test.sh") in python, a window pops up and asks me which program I want to open the test.sh with. Then python will end with output 0 and no folder is created in my C drive. I can't find any solution online. Any help will be appreciated. :)
os.system() will invoke your command the same as windows cmd would, in this case, the windows doesn't know how to execute *.sh files, so it opens it's default dialog so you can pick one program that you know can ran it.
The same will happen if you open windows terminal and try to invoke such file.
If your windows have a bash interpreter try invoking it like this:
os.system("bash Y:\test.sh")
Instead of running this with a native-Windows Python interpreter, run it with a Cygwin copy of Python, which has an os.system() that will be invoked with the Cygwin /bin/sh.
How to get an sh script for starting a new terminal, execute a python script and keep it running? The python script is supposed to run continuously in a perpetual loop, spitting out results as they pop in. Whenever trying with sh-script for gnome-terminal just getting: child process exited normally with status 2
Manually it would just be: python home/ubuntu/pyscript.py
Could someone give an idea how to do this?
I have a list of scripts to run, so resorting to the manual solution is tedious.
You can use gnome-terminal with the -x flag.
Suppose you have a spam.py script; then the following command will spawn a new terminal, run spam.py in it, and close the terminal once the script has ended.
gnome-terminal -x python spam.py
Try with this script:
# spam.py
import time
for _ in range(5):
print("eggs")
time.sleep(1)
Then the previous command will spawn a terminal, that will be printed eggs five times, and then will be closed.
If you want to leave the terminal open with the Python interpret still running after the script ended, then Python's -i flag (doc then CTRL+F -> -i) is what you want:
gnome-terminal -x python -i spam.py
To run the Python script in a new instance of your favourite terminal, write:
x-terminal-emulator -e python -i home/ubuntu/pyscript.py
This will start the Python script and run it until it ends, then display a Python prompt to stop the terminal emulator from closing.
This will work with x-terminal-emulator substituted with any of the many, many terminals installed on my computer, so will work with little modification across all POSIX-compatible systems with the standard terminals installed. This won't work on a Mac, however. For a properly cross-platform Python implementation of something slightly different, see here. Most of the techniques should be transferable.
To run the Python script in the same terminal whilst carrying on with the rest of the shell script, write:
python home/ubuntu/pyscript.py &
Note the &, which runs the program as a new process (but still connects the output to the virtual terminal).