This question already has answers here:
How to keep a Python script output window open?
(27 answers)
Closed 3 years ago.
In my Python script, I create a .bat file (many actually), and I run them via
os.startfile(blah)
Everything works like expected, however, those terminals die after finishing. I want to keep them open, so that I can type more commands manually in those opened terminals.
How?
You could try using the cmd command to run the batch file, and then use command line arguments for cmd to modify its behavior. For example:
os.startfile("cmd.exe /k blah.bat")
Documentation of the available command line arguments can be found here:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
Related
This question already has answers here:
How do I run a Python program in the Command Prompt in Windows 7?
(24 answers)
Closed 1 year ago.
I want to do is create a new python script so that I can start coding in python.
I am new to using windows command prompt. How can I access and create a new python script using the command prompt window? I have tried using cd (location of already made script). I have also just used data\scripts which is where the rest of the python scripts are located.
I have also attached a screenshot of what I attempted.
To access this in the CMD, you just need to type:-
cd data\scripts
Then you will be in your directory.
If you just want shell access, type:-
python
in the interface.
This question already has answers here:
How to use argv with Spyder
(4 answers)
Closed 2 years ago.
I'm using spyder for python programing.
I've a file.py and I want to run code like:
--model=RNN --optimizer=SGD
but when I run it in the spyder console it gives this error:
SyntaxError: can't assign to operator
So how can I run this?
In Spyder, if you go into the Run menu, you'll find an option named Configuration per file. On my system, you can get directly to that entry with CTRL+F6 (the shortcut key may be different on other OSs).
Once you select the menu item, you'll get a dialog window with several options. The one you want is about halfway down, named Command line options. You will want to check the box, and put the arguments you showed above into the adjacent text field. Now when you run the file, the code will behave as if those options were passed to it on the command line (it will see them in sys.argv).
This question already has answers here:
syntax error when using command line in python
(7 answers)
Closed 4 years ago.
I have just started learning python and make a python file called "helloworld.py" and store it inside a file called "python py" on the desktop, but this keep poping out whenever I am trying to open it in command line like this:
Can someone tell me what's wrong?
You are in a python shell (we can see that from >>>). Try starting cmd.exe and then run your same command:
python helloworld.py
(from the same path as the helloworld.py file).
When you run Python.exe (as you did), you can not run scripts. This is just for running commands "live". In your shell, try for example print(1). It should work.
This question already has answers here:
How do I pass a string into subprocess.Popen (using the stdin argument)?
(12 answers)
Closed 6 years ago.
I am able to open cmd.exe using script
import subprocess
subprocess.call("C:\Windows\System32\cmd.exe",shell=True)
But I am unable to send input command to cmd.exe open.
I want to achieve something like below mentioned using script
1) script give input command like python to cmd.exe open
2) After that script give input command like print "hello" to python prompt comes
Why not use Python to create a batch file that prints what you want, then execute that batch file? You could either return immediately or keep the command interpreter open: it depends on the command switches you use to run the batch file.
You could create a file hello.py containing print "hello", and run it through cmd ?
from subprocess import call
call(["python", "hello.py"])
This question already has answers here:
Request UAC elevation from within a Python script?
(12 answers)
Closed 7 years ago.
I'm writing a python script that will append a line in C:\Windows\System32\drivers\etc\hosts. How do I append a line in this file with administrator permission?
You append to a file by opening it in append mode:
with open('C:/Windows/System32/drivers/etc/hosts', 'a') as f:
f.write('127.127.127.127 static.ak.connect.facebook.com\n')
note: backslash is an escape character, so I use forward slashes, which the msvcrt.dll functions accept just as well
Finally, run your program as administrator in order to have write privileges. My knowledge of this is right-clicking in Windows explorer and choosing 'Run as Administrator'. You could run a command shell (cmd.exe) as administrator and run your python program from there. As far as Windows-specific UAC stuff, you'll need someone who is a Windows system programmer to help with that.