title: Move Certain Files from One Directory to Another - Python
body: <p>All,</p>
  
  <p>I need to move file from one directory to another but I don't want to move all the files in that directory just the text files that begin with <code>'pws'</code>. A <code>list</code> of all the files in the directory is: </p>
  
  <pre><code>['pws1.txt', 'pws2.txt', 'pws3.txt', 'pws4.txt', 'pws5.txt', 'x.txt', 'y.txt']
  </code></pre>
  
  <p>As stated, I want to move the <code>'pws*'</code> files to another directory but not the x and y text files. What I want to do is remove all elements the <code>list</code> that does not begin with <code>'pws'</code>.  My code is below: </p>
  
  <pre><code>loc = 'C:\Test1'
  dir = os.listdir(loc)
  
  #print dir
  
  for i in dir:
  #print i
  x = 'pws*'
  if i != x:
  dir.remove(i)
  print dir
  </code></pre>
  
  <p>The output does not keep what I want instead </p>
  
  <p>It removes the x text file from the <code>list</code> and the even number ones but retains the y text files. </p>
  
  <p>What am I doing wrong. How can I make a <code>list</code> of only the files that start with <code>'pws'</code> and remove the  text files that do not begin with <code>'pws'</code>. </p>
  
  <p>Keep in mind I might have a <code>list</code> that has 1000 elements and several hundreds of those elements will start with <code>'pws'</code> while those that don't begin with it, couple of hundreds, will need to be removed. </p>
  
  <p>Everyone's help is much appreciated.  </p>