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.