1

All,

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 'pws'. A list of all the files in the directory is:

['pws1.txt', 'pws2.txt', 'pws3.txt', 'pws4.txt', 'pws5.txt', 'x.txt', 'y.txt']

As stated, I want to move the 'pws*' files to another directory but not the x and y text files. What I want to do is remove all elements the list that does not begin with 'pws'. My code is below:

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

The output does not keep what I want instead

It removes the x text file from the list and the even number ones but retains the y text files.

What am I doing wrong. How can I make a list of only the files that start with 'pws' and remove the text files that do not begin with 'pws'.

Keep in mind I might have a list that has 1000 elements and several hundreds of those elements will start with 'pws' while those that don't begin with it, couple of hundreds, will need to be removed.

Everyone's help is much appreciated.

Mohd
  • 5,523
  • 7
  • 19
  • 30
duranil
  • 29
  • 1
  • 4
  • Possible duplicate of [How to move a file in Python](https://stackoverflow.com/questions/8858008/how-to-move-a-file-in-python) – dlmeetei Jul 16 '17 at 18:31

3 Answers3

2

You can use list-comprehension to re-create the list as the following:

dir = [i for i in dir if i.startswith('pws')]

or better yet, define that at start:

loc = 'C:\\Test1'
dir = [i for i in os.listdir(loc) if i.startswith('pws')]
print dir

Explanation:

When you use x = 'pws*' and then check for if i == x, you are comparing if the element i is equal to 'pws*', so a better way is to use str.startswith() built in method that will check if the string starts with the provided substring. So in your loop you can use if i.startswith('pws') or you can use list-comprehension as I mentioned above which is a more pythonic approach.

Mohd
  • 5,523
  • 7
  • 19
  • 30
  • Thank you. I ran the edited code, but I keep getting syntax error related to "for" in dir = [i for i in .... – duranil Jul 16 '17 at 22:54
  • @duranil change this line `loc = 'C:\Test1'` to `loc = 'C:\\Test1'` because you need to escape backlashes – Mohd Jul 16 '17 at 23:16
0

To move a file: How to move a file in Python You can use os.rename(origin,destination)

origin = r'C:\users\JohnDoe\Desktop'
destination = r'C:\users\JohnDoe\Desktop\Test'
startswith_ = 'pws'

And then go ahead and do a list comprehension

# Move files
[os.rename(os.path.join(origin,i),os.path.join(destination,i)) for i in os.listdir(origin) if i.startswith(startswith_)]
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
0

Just use a glob. This gives you a list of files in a directory without all the listdir calls and substring matching:

import glob,os
for f in glob.glob('/tmp/foo/pws*'):
    os.rename(f, '/tmp/bar/%s'%(os.path.basename(f)))

Edit 1: Here's your original code, simplified with a glob, and a new variable loc2 defined to be the place to move them to:

import os,glob
loc = 'C:\Test1'
loc2 = 'C:\Test2'
files = glob.glob('%s\pws*'%(loc))

for i in files:
    os.rename(i,'%s\%s'%(loc2,os.path.basename(i)))
CDahn
  • 1,795
  • 12
  • 23
  • @duranil, the code certainly does _something_. What part isn't working? Does the glob provide the list of files you expect? Does the os.rename not move them to the destination? – CDahn Jul 17 '17 at 01:25