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

Discussion in 'Python' started by Evan Carmi, Jan 2, 2007.

  1. Evan Carmi

    Evan Carmi Guest

    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?
     
    Evan Carmi, Jan 2, 2007
    #1
    1. Advertising

  2. Re: how to move files based on file-ending from dirs and subdirs to specific dir?

    In <>, 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
     
    Marc 'BlackJack' Rintsch, Jan 2, 2007
    #2
    1. Advertising

  3. Evan Carmi

    Evan Carmi Guest

    Re: how to move files based on file-ending from dirs and=?utf-8?b?c3ViZGlycwl0bwlzcGVjaWZpYw==?= dir?

    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
     
    Evan Carmi, Jan 2, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Fletcher
    Replies:
    2
    Views:
    426
    John Saunders
    Jul 31, 2003
  2. Prime
    Replies:
    6
    Views:
    1,934
  3. Florian Lindner

    Get all subdirs

    Florian Lindner, Aug 29, 2004, in forum: Python
    Replies:
    5
    Views:
    353
    Tim Peters
    Aug 29, 2004
  4. Thomas Hafner

    meaning of subdirs within $LOAD_PATH

    Thomas Hafner, Jan 26, 2007, in forum: Ruby
    Replies:
    1
    Views:
    97
  5. Kga Agk
    Replies:
    2
    Views:
    156
    Kga Agk
    Jun 29, 2009
Loading...

Share This Page