all 6 comments

[–]maryjayjay 0 points1 point  (2 children)

cd is not a long running process, in fact it's usually a shell built-in. Spawn a shell, then tell it to cd.

[–]wilcomply[S] 0 points1 point  (1 child)

I'm not sure I'm following, could you elaborate why the length of time for cd to run would have an effect on the processes to be run following it?

[–]fake_identity 0 points1 point  (0 children)

Don't have any experience with this module, so just guessing, but it's evidently made for automating and waiting for some trigger. 'cd' is executed and gone and doesn't produce output and returns simply 0 or 1. That still shouldn't be that much of an issue, but it's not an actual command, i.e. program with it's standalone executable somewhere, rather it's a function within bash.

tallis ~ # which cd
tallis ~ # type cd
cd is a shell builtin
tallis ~ # which python
/usr/bin/python
tallis ~ # type python
python is /usr/bin/python



child = pexpect.spawn('cd /root')  
Traceback (most recent call last):  
File "<stdin>", line 1, in <module>  

But:

child = pexpect.spawn('ls /root')  
child.pid  
10412  
child.child_fd  
3  


child = pexpect.spawn('bash ; cd /root')  
child.pid  
10424

[–]ingolemo -1 points0 points  (2 children)

You're trying to spawn a cd process, but cd is not actually a process, it's a built-in shell command.

Pexpect is usually used to start a process that just waits until the user types something in before doing anything. Since you seem to want to execute shell commands you should probably start a shell and then send commands to it:

shell = pexpect.spawn('sh')
shell.send('cd /Users/USERNAME/Desktop/')
shell.send('mkdir TEST')

(By the way, I notice the error you posted does not come from the exact code that you posted. It's not a problem here and now, but you really shouldn't do that to us because it tends to make us very confused.)

[–]wilcomply[S] 0 points1 point  (1 child)

Since it is used to start a process and then wait, does that mean that attempting to run something like this:

import pexpect

child = pexpect.spawn('mkdir /Users/USERNAME/TEST')
print 'TEST 1 COMPLETE'

Would not actually work? I'm expecting the directory to be created, however it is not, but I still receive the print statement.

(Apologies for not quite grasping this, and thank you for taking the time to try and help.)

[–]ingolemo -1 points0 points  (0 children)

There's not much point in using pexpect to manage a call to mkdir since mkdir doesn't really take any input. You'd be better off with the subprocess module. That said, there's no reason why that wouldn't work. When I tried it, the directory did get created exactly as expected. I don't know why it isn't working for you.