I was going through the pexpect documentation and it said You can call the run() function to execute a command and return the output
so i did the following but it errored out. Am i missing something here?
>>> pexpect.run('pwd')
'/home/vijay\r\n'
>>> pexpect.run('cd /home')
Traceback (most recent call last):
File "<stdin>", 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.
>>>
I’m writing a script on which I download a git repository and wanted to perform cd (to that repository) and then pass on some command. so I have decided to use pexpect module since it provides interactive console
Let me know if I’m missing anything here?
Thanks,
-Vijay
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
cwdkeyword arg: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 pexpectrunmethod:See: http://docs.python.org/library/os.html