简体   繁体   中英

how to input a respond to prompt of a command by python?

I ma running a Python code utilizing from prompt commands. It sometimes conflicts with the existing files and says

File 'outputs/g/Charlotte_s_Web_2006_-_Trailer.avi' already exists. Overwrite ? [y/N]

where the file name is changing.

Is it possible to capture that question and input N as an answer constantly on Python?

If you're running on some UNIX variant, you can do

yes N | <program>

which feeds a never-ending stream of "N"s to the program.

If you want to control a console-driven program from Python, consider using pexpect . This library enables very simple interaction:

import pexpect
pexpect.spawn("<cmd>")
while 1:
    child.expect(r'\[y/N\]')
    child.sendline('N')

If you're using python I would've though you could just look at the source... use Find to get to the line it inputs at and replace it with = "N".

As for capturing it I dont fully understand what you mean but I think it would be obvious, when you look at the source, where the variable is coming from and you could just save it to a file using the standard functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM