title: putting file names in an array using os in python
body: <p>I want to create a program that will use <code>os</code> to make an <code>ls</code> of a directory and then put the files in a array. However, <code>ls</code> does not list the names one by one, so I am kind of stuck, because there's nothing constant by which I can tell when the next name starts.</p>
  <p>Here is the actual code:</p>
  <pre><code>import os
  cd = input(&quot;Change directory to:   &quot;)
  
  while cd != &quot;x&quot;:
      os.chdir(cd)
      command = os.popen('ls')
      ls = command.read()
      print(ls)
      cd = input(&quot;Change directory to:   &quot;)
      count = 0
      word = &quot;&quot;
  
  for character in ls:
      while count == 0 :
          if character!= &quot; &quot;:
              count = 1
              word += character
  
          elif character == &quot; &quot;:
              print(word)
  </code></pre>
  <p>So the idea is that, when the program starts, it should ask you which directory you want to change to and then run <code>ls</code> on that directory. Then if you type back <code>x</code>, it should just print the first directory to the screen. However, as I said, it just prints the entire list.</p>
  <p>Can anybody help me with this?</p>
  <p>Any help appreciated. Thanks!</p>