By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Jan 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In <ma***************************************@python. org>, Evan Carmi
wrote:
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
Jan 2 '07 #2

P: n/a
Marc 'BlackJack' Rintsch <bj_666 <atgmx.netwrites:
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
Jan 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.