0

Does anyone know if it is possible to manipulate the output of an exe when calling it from the native os module in python? For example suppose we have some executable, call it example.exe, and when it is executed it asks the user for input, a file perhaps.

os.system('example.exe') Enter file name: <--- this is the output from example.exe that requires user input, i.e. I have to type the file name in

What I would like to be able to do is generate a list of the files in the cwd and then enter the files one by one as I call the script.

file_list = os.listdir(os.getcwd()) for f in file_list: os.system('example.exe') Enter file name: f

But I am unsure if this is possible because it requires user input and I am not sure how to mimic that from script.

Update: The issues arises because the example.exe has no option for providing arguments, e.g. I cannot do something like example.exe filename to provide the required input.

Cauchy
  • 71
  • 6
  • Please check if `example.exe` supports parameters to hand over the filename, if yes then you can use python subprocess https://docs.python.org/2/library/subprocess.html – admirableadmin Jun 13 '16 at 14:39
  • it doesnt, sorry I forgot to mention that – Cauchy Jun 13 '16 at 14:57
  • Not easily -- but you could do it. `subprocess` allows for greater control that `os.system`. Launch `example.exe`, suppress 'Enter file name:`, output your own prompt, collect stdin from user and force feed that into `example.exe`'s stdin. – user590028 Jun 13 '16 at 15:01
  • so I could force the output into a text file, but forcing the input has alluded me. As per [link](https://docs.python.org/2/library/subprocess.html#frequently-used-arguments) I see that stdin can be an existing file object and so I thought I would be able to simply pass a file object but I receive an error message I have never encountered before: – Cauchy Jun 14 '16 at 16:20
  • The error is as follows:`Incrementally linked image--PC correlation disabled. forrtl: severe (43): file name specififcation error, unit 15` – Cauchy Jun 14 '16 at 16:34
  • So I believe the error arises because the executable I run is looking for a string and subprocess.call([],stdin) requires a file descriptor, object or pipe and so if I pass a string I will get an error, but if I pass an object or descriptor I also receive an error – Cauchy Jun 15 '16 at 19:14

0 Answers0