send python variable to powershell and vice-versa - python

i would like to know how to send a variable from python to powershell and vice-versa:
I'm writing a code to get the infos of a install.wim file in the windows installation disk.
I want a result like this:
(python input) path = input("WIM file path: ");
(powershell background command) $wim_infos = Get-WindowsImage -ImagePath $python_input_path;
(powershell command) py .\main.py $wim_infos;
(python command) print('Your WIM file infos:');
(python output) print(sys.argv[1]);
i tried to code this but i dont know how to send the python input path variable to powershell...
sorry if I wasn't clear, I made some mistakes, or I asked for something obvious, but I'm not very good at these things...

Related

How can I use Python to open command prompt, change directories then pass a string that contains a variable?

First time ever asking for help here so my apologies if i'm missing something relevant.
I am learning Python for data science (very new), but figured I could use it to automate some of my other work.
I'll post what I have so far, but first I'll explain what I'm trying to do:
Normally (without Python) I open up the command prompt, change directories, and then paste a string (directly into command prompt) that looks like:
"program.exe C:\file path to config file\config.cfg -extra_parameters1 -extra_parameters2 -extra_parameters3 -Mail_Date=YYYY-MM-DD"
here is what I have so far:
import os
mail_date = input(("What is the mailing date?(YYYY-MM-DD): "))
os.system("start cmd.exe #cmd /k CD C:\Path")
# Next string would look like:
# "program.exe C:\file path to config file\config.cfg -extra_parameters1 -extra_parameters2 -extra_parameters3 -Mail_Date=YYYY-MM-DD
So far I can run the Python script, input a mailing date as a variable, open command prompt, then change directories. I am not sure how to then pass the "program.exe..." string to the command line.

What creating batch file fails in my python script?

I am studying automate the boring stuff with python book and therein after finishing the password locker project it is suggested to create a batch file using the following codes:
#py.exe C:\Python34\pw.py %*
#pause
and I did exactly the same mine one looks like this (copied entire script if there are any other potential errors):
import sys
import pyperclip
""" PASSWORD LOCKER """
passwords = {
'facebook' : 'password of facebook',
'gmail' : 'password of gmail',
'quora' : 'password of quora'
}
if len(sys.argv) == 2:
account_name = sys.argv[1]
if account_name in passwords:
print('Password of [' + str(account_name).upper() + '] has been copied to clipboard.')
acc_password = passwords[account_name]
pyperclip.copy(acc_password)
print('Paste it anywhere')
else:
print('There is no name registered with that account name.')
#py.exe 'C:\py\Automate the Boring Stuff with Python\Data Structures, Dictionary\pw.py' %*
#pause
then saved the file as pw.bat, according to instruction of the book:
With this batch file created, running the password-safe program on
Windows is just a matter of pressing win-R and typing pw .
I then followed these steps again and it didn't work fine. Can you please help me with this. Thank you.
Here are troubleshooting steps I'd suggest:
Ensure py.exe exists in environment path of your computer. The book may have installation instructions that may have been skipped inadvertently. If you know py.exe exists on your computer, but when you execute py.exe on opening a command prompt, you may see:
'py.exe' is not recognized as an internal or external command, operable program or batch file.
If you see the above message, this implies, you need to do either of the following:
a. Change your batch file to say:
`#<drive:\path\to>\py.exe`
where the part <drive:\path\to> is the path to your python executable (py.exe), OR,
b. Add the path to py.exe to the environment variables. Comprehensive instructions are here for Python 3.
The way you have framed the question seems to suggest that you saved the entire code in one file pw.bat. If you did, you need to ensure the following:
a. Last two lines of the your code snippet are in a file pw.bat, AND,
b. Rest of the code is in the file C:\py\Automate the Boring Stuff with Python\Data Structures, Dictionary\pw.py.
The instruction below is correct only if the file pw.bat is in the environment path and thus visible to the operating system:
With this batch file created, running the password-safe program on Windows is just a matter of pressing win-R and typing pw
If you do not know whether this is true or not, you will need to find out where you saved pw.bat. Open a command prompt and navigate to that location, and then execute pw. Issues pertinent to this are discussed here.

Learn Python the Hard Way, Ex 2; Powershell not outputting ex1.py?

I am very new to the language so I might need an ELI5 for the response.
I've created my ex1.py, checked all of the casing and syntax and it appears to be right?
print "Hellow World!"
print "Hello again"
print "i like typing this"
print "this is fun"
print 'Yay! printing!'
print "I'd much rather you 'n'."
print 'I "said" do not touch this!'
When I type: python ex1.py in terminal after navigating to the proper folder, the terminal displays the script, not powershell.
I've tried entering
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
etc. string that is on page 8 of the book into PowerShell, but that doesn't seem to help.
In the filepath above, should I be replacing C:\Python27 with the literal filepath of where i have Python installed? Likewise, "User" with my username? Or do I enter the string literally as is?
Your code is fine but it seems your environment is not set correctly. Do the following:
Start powershell
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\<directory_where_python.exe in installed>", "User")
Restart powershell (close it, then start it back up again)
Cd in directory that contains ex1.py
Execute python .\ex1.py
Please see if you have saved the file ex1.py properly.Check once.
You may have saved the file name first and later typed in the notepad and forgot to save the file.

Call a cmd.exe bat.-like command with python

i need some help with this...
I have a program installed on my computer that i want to call to calculate some things and give me an output-file...
in Matlab the command "dos()" does the job giving me also the cmd screen output in matlab.
I need this to work in python but i am making something wrong.
data='file.csv -v'
db=' -d D:\directory\bla\something.db'
anw='"D:\Program Files\bla\path\to\anw.exe"' + db + ' -i' + data
"anw" output is this one:
>>> anw
'"D:\\Program Files\\bla\\path\\to\\anw.exe" -d D:\\directory\\bla\\something.db -i file.csv -v'
## without the "" it does not work either
import subprocess as sb
p= sb.Popen('cmd','/K', anw) ## '/C' does not work either
i get the following error message from cmd.exe inside the python shell
Windows cannot find "\"D:\Program Files\bla\path\to\anw.exe"" Make sure you typed the name correctly, and then try again.
this line runs when i make a bat. file out of it.
it runs in matlab via "dos(anw)" so what is wrong here?
ps: i have blanks in my command... could this be the problem? i do not know where the first "\" comes from in the cmd. exe error message
for now i created a bat. file with all the stuff cmx.de should do in the specific directory where the input file lies...
i just had to tell python to change directory with
import os
os.chdir("D:\working\directory")
os.system(r'D:\working\directory\commands.bat')
it works good and gives me the output of cmd directly in the python shell

Syntax for input redirection in IDLE

I need to enter the contents of a text (.txt) file as input for a Python (.py) file. Assuming the name of the text file is TextFile and the name of the Python file PythonFile, then the code should be as follows:
python PythonFile.py < TextFile.txt
Yet, when I try to do this in IDLE and type in
import PythonFile < TextFile,
IDLE gives me an invalid syntax message, pointing to the < sign. I tried all sorts of variations on this theme (i.e.,using or not using the file name extensions), but still got the same invalid-syntax message. How is the syntax different for input redirection in IDLE?
If it works in the command line, then why do you want to do this in IDLE? There are ways to achieve a similar result using, for example, subprocess, but a better way would be to refactor PythonFile.py so that you can call a function from it, e.g.:
>>> import PythonFile
>>> PythonFile.run_with_input('TextFile.txt')
If you post the contents of PythonFile.py, we might be able to help you do this.

Categories