Need help using python subprocess module to navigate Linux directories
<p>First off, I know there are FAR better ways to do this. I'm trying to learn the most basic behavior of subprocess.Popen() when it interacts with various UNIX commands. I'm doing something wrong with directory navigation, and I have no idea what it is. I'm running iPython as my REPL, so the ls command shows the files in the current working directory. </p>

<p>Someone please tell me what I'm doing wrong! </p>

<pre><code>In [61]: newtree_dirs
Out[61]:
['10dir',
 '1dir',
 '2dir',
 '3dir',
 '4dir',
 '5dir',
 '6dir',
 '7dir',
 '8dir',
 '9dir']

In [62]: ls
10dir/  1dir/  2dir/  3dir/  4dir/  5dir/  6dir/  7dir/  8dir/  9dir/

In [63]: for folder in newtree_dirs:
    ...:     p1 = sub.Popen(['cd', './{}'.format(folder)])
    ...:     p1.communicate()
    ...:     foo = (i for i in xrange(10))
    ...:     for num in foo:
    ...:         p2 = sub.Popen(['touch', '{}file'.format(num)])
    ...:         p2.communicate()
    ...:     p3 = sub.Popen(['cd', '..'])
    ...:     p3.communicate()
    ...:
    ...:
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
&lt;ipython-input-63-bb6e77faf97b&gt; in &lt;module&gt;()
      1 for folder in newtree_dirs:
----&gt; 2     p1 = sub.Popen(['cd', u'./{}'.format(folder)])
      3     p1.communicate()
      4     bar = (i for i in xrange(10))
      5     for num in bar:
</code></pre>

<p>after that exception, further exceptions descend into the subprocess module's error handling for a missing directory. The directory names in my cwd are identical and I don't know what's going on. </p>

<p><code>sub.Popen(['cd', './10dir')</code> raises "OSError: [Errno 2] No such <strong>file</strong> or directory" because there is no such file named <code>cd</code> on your <code>$PATH</code> (and possibly on your computer at all). <code>cd</code> is not a stand alone executable, it's a shell builtin. If it were a stand alone executable it wouldn't be able to change the current working directory of the shell (or of your script for that matter) because a child process can't directly change it's parent's current working directory, environment variables, user id etc.</p>

<p>More info:<br>
<a href="//unix.stackexchange.com/q/11454/142240">builtin vs normal command</a></p>

<p>Note: Technically to be POSIX complient in addition to the shell builtin <code>cd</code> an OS must provide a stand alone executable <code>cd</code> that changes <strong>it's own</strong> current directory and returns, but many Linux distributions don't include it. Source: <a href="//unix.stackexchange.com/a/38819/142240">Why is cd not a program?</a></p>

python linux shell