Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to move files based on file-ending from dirs and subdirs tospecific dir?

Reply
Thread Tools

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

 
 
Evan Carmi
Guest
Posts: n/a
 
      01-02-2007
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?



 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      01-02-2007
In <(E-Mail Removed)>, 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
 
Reply With Quote
 
 
 
 
Evan Carmi
Guest
Posts: n/a
 
      01-02-2007
Marc 'BlackJack' Rintsch <bj_666 <at> gmx.net> writes:

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
meaning of subdirs within $LOAD_PATH Thomas Hafner Ruby 1 01-26-2007 03:24 PM
Program to list all dirs, subdirs and files older than 1 year Prime Java 6 12-15-2005 08:56 AM
Get all subdirs Florian Lindner Python 5 08-29-2004 03:17 PM
how to reference assemblies from web.configs in subdirs? Lucas Fletcher ASP .Net 2 07-31-2003 10:50 AM



Advertisments