I'm relatively new to Python and was interested in learning if it's possible to interact with the Windows Command Line with Python (meaning able to directly input commands, such as dir
or cd
). I ask as I'm interested in making scripts to run in a Windows environment. I'm aware of the subprocess module, but I'm not sure if it has a routine for direct input.
Thanks.
Asked
Active
Viewed 68 times
0

foobiane
- 1
-
1It sounds like you want the `pexpect` module. From the [documentation](https://pexpect.readthedocs.io/en/stable/): "Pexpect allows your script to spawn a child application and control it as if a human were typing commands." – John Gordon Nov 21 '19 at 21:44
-
[_Calling an external command from Python_](https://stackoverflow.com/questions/89228/calling-an-external-command-from-python) might answer your question. – Gerd Nov 22 '19 at 08:57
-
Yes, but this is extremely limited -- at least prior to Windows 10 w/ pseudoconsole support -- because you'll be communicating over pipes. This means most external commands will switch to full buffering of their standard output, which makes interaction impossible. On a case-by-case basis you have to determine the command-line option or environment variable, if any, that forces a program to use no buffering or line buffering. Windows doesn't have anything like Linux `stdbuf` to work around this problem. – Eryk Sun Nov 22 '19 at 10:15