how to move files based on file-ending from dirs and subdirs tospecific dir?

E

Evan Carmi

hey,

i am trying to move files with a specific file-ending (.msf) to dir above their
current location. my code so far works as long as all the files are on the same
dir level.

but how can i make it work if there are multiple subdirs with files inside of
them on different dir levels?


my code so far is:
---
top = 'f:\\test\\mail'


import os, time, itertools

#walk and find all files
allfiles = []
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
allfiles.append(os.path.join(root, name))
#remove all non .msf files
index = []
for match in allfiles:
if match.endswith('.msf'):
index.append(match)

indexdest = []
indexdest = ['%s\\..\\..\\%s\\%s\\%s' % (x , time.strftime('%Y%m%d%H%M%S'),
os.path.basename(os.path.normpath(x+'\\..')), os.path.basename(x)) for x in ind
ex]

#\\.. to remove the ending and than basename to add it back on
indexdest = [os.path.normpath(i) for i in indexdest]
indexdest = [i.replace('Mail', 'backups-msf') for i in indexdest]

for a, b in itertools.izip(index, indexdest):
os.renames(a, b)
--

i want the .msf files to be in a dir on the same level as where the top string
points to (in this case f:\test\backups-msf).

thanks,
Evan

p.s. is there a more accurate word for a directory level or something on the
same "tier" of a directory?
 
M

Marc 'BlackJack' Rintsch

Evan Carmi said:
top = 'f:\\test\\mail'

[…]

indexdest = []

Here you bind the name `indexdest` to an empty list but replace it in the
very next line with another list. So this line is unnecessary.
indexdest = ['%s\\..\\..\\%s\\%s\\%s' % (x , time.strftime('%Y%m%d%H%M%S'),
os.path.basename(os.path.normpath(x+'\\..')), os.path.basename(x)) for x in ind
ex]

This line is quite complicated and I don't really grasp what it's doing.
Maybe some comments, possible with examples, are needed here. And maybe
then you'll see why it doesn't work as wanted!? If these are the
destination paths and they should be at the same level as `top`, where is
`top` here? Shouldn't that be a prefix of all destination paths?

Ciao,
Marc 'BlackJack' Rintsch
 
E

Evan Carmi

Marc 'BlackJack' Rintsch said:
This line is quite complicated and I don't really grasp what it's doing.
Maybe some comments, possible with examples, are needed here. And maybe
then you'll see why it doesn't work as wanted!? If these are the
destination paths and they should be at the same level as `top`, where is
`top` here? Shouldn't that be a prefix of all destination paths?

Ciao,
Marc 'BlackJack' Rintsch

I figured out the problem, with a lot of help.
Thanks for the help
Evan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top