python - sending more commands to pexpect child after spawn -
i learning how use pexpect. goal getting list of directories, recreate directory in folder using pexpect. however, how send multiple of commands pexpect child in python loop? child.sendline()
doesnt work me =[. have been respawning child, doesnt seem proper way of doing it.
import pexpect child = pexpect.spawn("""bash -c "ls ~ -l | egrep '^d'""") child.expect(pexpect.eof) templist = child.before templist = templist.strip() templist = templist.split('\r\n') listofnewfolders = [] folder in templist: listofnewfolders.append(folder.split(' ')[-1]) folder in listofnewfolders: child.sendline("""bash -c 'mkdir {0}'""".format("~/newfolder/%s" % folder))
if bash -c
, bash run command specify, , exit. send multiple commands, you'll need this:
p = pexpect.spawn('bash') p.expect(prompt_regex) p.sendline('ls') # instance p.expect(prompt_regex) print(p.before) # output last command.
Comments
Post a Comment