I'm working on windows vista, but I'm running python from DOS command. I have this simple python program. (It's actually one py file named test.py)
import os
os.system('cd ..')
When I execute "python test.py" from a Dos command, it doesn't work.
For example, if the prompt Dos Command before execution was this:
C:\Directory>
After execution, must be this:
C:\>
Help Plz.
First, you generally don't want to use os.system - take a look at the subprocess module instead. But, that won't solve your immediate problem (just some you might have down the track) - the actual reason cd won't work is because it changes the working directory of the subprocess, and doesn't affect the process Python is running in - to do that, use os.chdir.
I don't really use Windows, but you can try cmd /k yourcommandhere. This executes the command and then returns to the CMD prompt.
So for example, maybe you can do what you want like this:
subprocess.call(['cmd', '/k', 'cd .. && prompt changed'])
As I said, I am not familiar with Windows, so the syntax could be wrong, but you should get the idea.
In case you don't know, this is a different CMD instance than the one you were in before you started your python script. So when you exit, your python script should continue execution, and after it's done, you'll be back to your original CMD.
Related
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"))
My python script generate a proper command that user need to run in the same console. My scenario is that user is running a script and then as a result see the command that must to run. Is there any way to exit python script and send that command to console, so user do not need to copy/paste?
A solution would be to have your python script (let's call it script.py) just print the command: print('ls -l') and use it in a terminal like so: $(python3 script.py). This makes bash run the output of your script as a command, and would basically run a ls -l in the terminal.
You can even go a step beyond and create an alias in ~/.bashrc so that you no longer need to call the whole line. You can write at the end of the file something like alias printls=$(python3 /path/to/script.py). After starting a new terminal, you can type printls and the script will run.
A drawback of this method is that you have no proper way of handling exceptions or errors in your code, since everything it prints will be run as a command. One way (though ugly) would be to print('echo "An error occured!"') so that the user who runs the command can see that something malfunctioned.
However, I'd suggest going for the "traditional" way and running the command directly from python. Here's a link to how you can achieve this: Calling an external command in Python.
Python can run system commands in new subshells. The proper way of doing this is via the subprocess module, but for simple tasks it's easier to just use os.system. Example Python script (assuming a Unix-like system):
import os
os.system('ls')
I need to launch external program from python script, which can be run in command prompt. I've been searching through python documentations and stack overflow but can't find anything helpful to me. I successfully launch cmd with following script:
But I still need to write down more commands like that:
mkdir data
copy data.txt c:\data
I think this is a very easy job with subprocess module but I can't find the way. How can I do this?
Try to use call
subprocess.call(['cmd.exe', 'mkdir data']);
In general, we can use Python to execute Windows's cmd command, for example:
os.system('ipconfig')
but I find that tskill can not be executed by Python, if I use:
os.system('tskill 8684')
to kill a process by its pid, Python will show cmd's error:
'tskill' is not recognized as an internal or external command, operable program or batch file.
but it work well if I use cmd to run the command.
As I know tskill.exe is located in C:\Windows\System32, but this path is not in Python's environment variable. It is maybe the reason, but ipconfig.exe is also in System32, it can be executed.
So why tskill can not be executed by os.system or subprocess.Popen?
I have found the root reason:
My Python is 32-bit, while My PC is Windows7 64-bit, so Python's os.system can not run tskill. If I use Python 64-bit instead, everything is OK.
Use taskkill, which can do pretty much everything as tskill
But if you want to stick to tskill.exe in your scripts/code. Please run the scripts from elevated command prompts. (Right click on cmd.exe and run it as administrator)
os.system('c:\windows\system32\tskill.exe 8684')
Obviously it helps to have the interpreter to debug, but I prefer to execute commands in terminal. Is there any way to make it run the python program, then startup an interpreter with the variables and functions created already in there. My current command is this:
python main.py < tests/1.in
Does anyone know how to modify it to make the variables and functions accessible after runtime?
Use the -i flag:
python -i main.py < tests/1.in
Houw about -i:
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x