html tool

2017年11月19日星期日

pexpect use cd

https://stackoverflow.com/questions/11289602/a-new-bie-clarification-on-pexpect-module

cd is a builtin function of your shell. You can run whatever command you want to do in whatever directory you want to using the run() methods cwd keyword arg:
pexpect.run("pwd", cwd="/home")
For more information check the API docs at: http://pexpect.sourceforge.net/pexpect.html
Alternatively you can use Python's os.chdir() function to change the current working directory before you execute the pexpect run method:
os.chdir("/home")
pexpect.run("pwd")

[?]
>>> pexpect.run('pwd')
'/home/vijay\r\n'
>>> pexpect.run('cd /home')
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 219, in run
    child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env)
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 429, in __init__
    self._spawn (command, args)
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 516, in _spawn
    raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
pexpect.ExceptionPexpect: The command was not found or was not executable: cd.
>>> 

没有评论:

发表评论