Finding and copying files with python.

Discussion in 'Python' started by gtb, Apr 3, 2007.

  1. gtb

    gtb Guest

    I wish to copy the highest version number of a file from directory \
    \
    \fileserver\D:\scripts to C:\scripts where the file names are of the
    form

    filename_MM.NN.SS.zip, where MM, NN, and SS can be one to three
    digits.

    Example directory:
    other.zip
    dx_ver_1.1.63.zip
    dx_ver_1.2.01.zip
    dx_ver_1.12.7.zip
    temp.txt


    Does python have string matching routines that would find the bottom
    listed zip file and/or file copying routines?

    A little pointer or two would be much appreciated.

    Thanks,

    jh
     
    gtb, Apr 3, 2007
    #1
    1. Advertising

  2. gtb

    Guest

    Just sort them and then select the bottom one from a list by using a
    negative indices. I.e.:

    list[-1]

    Would return the bottom result out of a list

    On Apr 3, 2:21 pm, "gtb" <> wrote:
    > I wish to copy the highest version number of a file from directory \
    > \
    > \fileserver\D:\scripts to C:\scripts where the file names are of the
    > form
    >
    > filename_MM.NN.SS.zip, where MM, NN, and SS can be one to three
    > digits.
    >
    > Example directory:
    > other.zip
    > dx_ver_1.1.63.zip
    > dx_ver_1.2.01.zip
    > dx_ver_1.12.7.zip
    > temp.txt
    >
    > Does python have string matching routines that would find the bottom
    > listed zip file and/or file copying routines?
    >
    > A little pointer or two would be much appreciated.
    >
    > Thanks,
    >
    > jh
     
    , Apr 3, 2007
    #2
    1. Advertising

  3. gtb

    Guest

    On Apr 3, 8:21 am, "gtb" <> wrote:
    > I wish to copy the highest version number of a file from directory \
    > \
    > \fileserver\D:\scripts to C:\scripts where the file names are of the
    > form
    >
    > filename_MM.NN.SS.zip, where MM, NN, and SS can be one to three
    > digits.
    >
    > Example directory:
    > other.zip
    > dx_ver_1.1.63.zip
    > dx_ver_1.2.01.zip
    > dx_ver_1.12.7.zip
    > temp.txt
    >
    > Does python have string matching routines that would find the bottom
    > listed zip file and/or file copying routines?
    >
    > A little pointer or two would be much appreciated.
    >
    > Thanks,
    >
    > jh


    You could just use string slicing to cut off the first 7 characters
    and have the numbers available to compare. There's also the os.stat
    module to find the last modified date of the file. You might be able
    to use the glob module to grab a list of the files and then sort the
    list too.

    Mike
     
    , Apr 3, 2007
    #3
  4. gtb

    gtb Guest

    On Apr 3, 8:31 am, wrote:
    > On Apr 3, 8:21 am, "gtb" <> wrote:
    >
    >
    >
    >
    >
    >
    >
    > > I wish to copy the highest version number of a file from directory \
    > > \
    > > \fileserver\D:\scripts to C:\scripts where the file names are of the
    > > form

    >
    > > filename_MM.NN.SS.zip, where MM, NN, and SS can be one to three
    > > digits.

    >
    > > Example directory:
    > > other.zip
    > > dx_ver_1.1.63.zip
    > > dx_ver_1.2.01.zip
    > > dx_ver_1.12.7.zip
    > > temp.txt

    >
    > > Does python have string matching routines that would find the bottom
    > > listed zip file and/or file copying routines?

    >
    > > A little pointer or two would be much appreciated.

    >
    > > Thanks,

    >
    > > jh

    >
    > You could just use string slicing to cut off the first 7 characters
    > and have the numbers available to compare. There's also the os.stat
    > module to find the last modified date of the file. You might be able
    > to use the glob module to grab a list of the files and then sort the
    > list too.
    >
    > Mike


    Thanks for posting folks. I didn't make my question clear. Before I
    sort the files I need to ensure that I am only sorting the files that
    match the profile of "filename_MM.NN.SS.zip", where MM, NN, and SS can
    be one to three
    digits.

    Thanks again,

    jh
     
    gtb, Apr 3, 2007
    #4
  5. gtb

    Guest

    On Apr 3, 8:47 am, "gtb" <> wrote:
    > On Apr 3, 8:31 am, wrote:
    >
    >
    >
    > > On Apr 3, 8:21 am, "gtb" <> wrote:

    >
    > > > I wish to copy the highest version number of a file from directory \
    > > > \
    > > > \fileserver\D:\scripts to C:\scripts where the file names are of the
    > > > form

    >
    > > > filename_MM.NN.SS.zip, where MM, NN, and SS can be one to three
    > > > digits.

    >
    > > > Example directory:
    > > > other.zip
    > > > dx_ver_1.1.63.zip
    > > > dx_ver_1.2.01.zip
    > > > dx_ver_1.12.7.zip
    > > > temp.txt

    >
    > > > Does python have string matching routines that would find the bottom
    > > > listed zip file and/or file copying routines?

    >
    > > > A little pointer or two would be much appreciated.

    >
    > > > Thanks,

    >
    > > > jh

    >
    > > You could just use string slicing to cut off the first 7 characters
    > > and have the numbers available to compare. There's also the os.stat
    > > module to find the last modified date of the file. You might be able
    > > to use the glob module to grab a list of the files and then sort the
    > > list too.

    >
    > > Mike

    >
    > Thanks for posting folks. I didn't make my question clear. Before I
    > sort the files I need to ensure that I am only sorting the files that
    > match the profile of "filename_MM.NN.SS.zip", where MM, NN, and SS can
    > be one to three
    > digits.
    >
    > Thanks again,
    >
    > jh


    Then you probably need to use the glob module and the re module. You
    may even be able to just use the glob module by doing something like:

    filenames = glob.glob(r'pathtofiles\*.*.*.*.zip')

    and then sort that.

    Mike
     
    , Apr 3, 2007
    #5
  6. gtb

    Steve Holden Guest

    gtb wrote:
    > On Apr 3, 8:31 am, wrote:
    >> On Apr 3, 8:21 am, "gtb" <> wrote:
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>> I wish to copy the highest version number of a file from directory \
    >>> \
    >>> \fileserver\D:\scripts to C:\scripts where the file names are of the
    >>> form
    >>> filename_MM.NN.SS.zip, where MM, NN, and SS can be one to three
    >>> digits.
    >>> Example directory:
    >>> other.zip
    >>> dx_ver_1.1.63.zip
    >>> dx_ver_1.2.01.zip
    >>> dx_ver_1.12.7.zip
    >>> temp.txt
    >>> Does python have string matching routines that would find the bottom
    >>> listed zip file and/or file copying routines?
    >>> A little pointer or two would be much appreciated.
    >>> Thanks,
    >>> jh

    >> You could just use string slicing to cut off the first 7 characters
    >> and have the numbers available to compare. There's also the os.stat
    >> module to find the last modified date of the file. You might be able
    >> to use the glob module to grab a list of the files and then sort the
    >> list too.
    >>
    >> Mike

    >
    > Thanks for posting folks. I didn't make my question clear. Before I
    > sort the files I need to ensure that I am only sorting the files that
    > match the profile of "filename_MM.NN.SS.zip", where MM, NN, and SS can
    > be one to three
    > digits.
    >
    > Thanks again,
    >
    > jh
    >

    OK, well look at the glob module to get a list of the filenames.

    You are going to have to be careful doing the sort, however, because a
    simple string comparison won't work for numbers of differing lengths.

    One way would be to define a function that extracts the numeric
    components from a filename and produces a three-element tuple or list.
    Then pass this function to sort() as the "key" argument.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Recent Ramblings http://holdenweb.blogspot.com
     
    Steve Holden, Apr 3, 2007
    #6
  7. > > > I wish to copy the highest version number of a file from directory \
    > > > \
    > > > \fileserver\D:\scripts to C:\scripts where the file names are of the
    > > > form

    >
    > > > filename_MM.NN.SS.zip, where MM, NN, and SS can be one to three
    > > > digits.

    >
    > > > Example directory:
    > > > other.zip
    > > > dx_ver_1.1.63.zip
    > > > dx_ver_1.2.01.zip
    > > > dx_ver_1.12.7.zip
    > > > temp.txt

    >
    > > > Does python have string matching routines that would find the bottom
    > > > listed zip file and/or file copying routines?


    > > You could just use string slicing to cut off the first 7 characters
    > > and have the numbers available to compare. There's also the os.stat
    > > module to find the last modified date of the file. You might be able
    > > to use the glob module to grab a list of the files and then sort the
    > > list too.


    Comparing the version strings is not enough: you have to convert the
    parts into integers, because else:

    >>> "dx_ver_1.12.7.zip" < "dx_ver_1.2.1.zip"

    True

    > Thanks for posting folks. I didn't make my question clear. Before I
    > sort the files I need to ensure that I am only sorting the files that
    > match the profile of "filename_MM.NN.SS.zip", where MM, NN, and SS can
    > be one to three
    > digits.


    Match the file names against the pattern "dx_ver_(\d+).(\d+).(\d
    +).zip". You may also use the glob function, but then you will have to
    parse the version number from the file name anyway: with the regexp
    you can use match.groups() to get the version number.

    You can do:

    import re
    ver_re = re.compile(r"dx_ver_(\d+).(\d+).(\d+).zip")

    def getVer(fn):
    """Return a *comparable* file version, None for bad file names"""
    m = ver_re.match(fn)
    return m and map(int, m.groups())

    print sorted(os.listdir('/path/to/wherever'), key=getVer)[-1]

    --Daniele

    P.S. I guess in Obfuscated Python one would write something like:

    >>> print sorted((pair for pair in ((re.match(r"dx_ver_(\d+).(\d+).(\d+).zip", fn), fn) for fn in os.listdir('/path/to/wherever')) if pair[0]), key=lambda _: map(int, _[0].groups()))[-1][1]

    dx_ver_1.12.7.zip
     
    Daniele Varrazzo, Apr 3, 2007
    #7
  8. gtb

    gtb Guest

    On Apr 3, 9:42 am, "Daniele Varrazzo" <>
    wrote:
    > > > > I wish to copy the highest version number of a file from directory \
    > > > > \
    > > > > \fileserver\D:\scripts to C:\scripts where the file names are of the
    > > > > form

    >
    > > > > filename_MM.NN.SS.zip, where MM, NN, and SS can be one to three
    > > > > digits.

    >
    > > > > Example directory:
    > > > > other.zip
    > > > > dx_ver_1.1.63.zip
    > > > > dx_ver_1.2.01.zip
    > > > > dx_ver_1.12.7.zip
    > > > > temp.txt

    >
    > > > > Does python have string matching routines that would find the bottom
    > > > > listed zip file and/or file copying routines?
    > > > You could just use string slicing to cut off the first 7 characters
    > > > and have the numbers available to compare. There's also the os.stat
    > > > module to find the last modified date of the file. You might be able
    > > > to use the glob module to grab a list of the files and then sort the
    > > > list too.

    >
    > Comparing the version strings is not enough: you have to convert the
    > parts into integers, because else:
    >
    > >>> "dx_ver_1.12.7.zip" < "dx_ver_1.2.1.zip"

    >
    > True
    >
    > > Thanks for posting folks. I didn't make my question clear. Before I
    > > sort the files I need to ensure that I am only sorting the files that
    > > match the profile of "filename_MM.NN.SS.zip", where MM, NN, and SS can
    > > be one to three
    > > digits.

    >
    > Match the file names against the pattern "dx_ver_(\d+).(\d+).(\d
    > +).zip". You may also use the glob function, but then you will have to
    > parse the version number from the file name anyway: with the regexp
    > you can use match.groups() to get the version number.
    >
    > You can do:
    >
    > import re
    > ver_re = re.compile(r"dx_ver_(\d+).(\d+).(\d+).zip")
    >
    > def getVer(fn):
    > """Return a *comparable* file version, None for bad file names"""
    > m = ver_re.match(fn)
    > return m and map(int, m.groups())
    >
    > print sorted(os.listdir('/path/to/wherever'), key=getVer)[-1]
    >
    > --Daniele
    >
    > P.S. I guess in Obfuscated Python one would write something like:
    >
    > >>> print sorted((pair for pair in ((re.match(r"dx_ver_(\d+).(\d+).(\d+).zip", fn), fn) for fn in os.listdir('/path/to/wherever')) if pair[0]), key=lambda _: map(int, _[0].groups()))[-1][1]

    >
    > dx_ver_1.12.7.zip


    Thanks all. Much to learn, but you have certainly helped me get
    started.

    jh
     
    gtb, Apr 3, 2007
    #8
    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. Replies:
    0
    Views:
    512
  2. Russell Lear

    Copying files in Python

    Russell Lear, Jul 3, 2003, in forum: Python
    Replies:
    0
    Views:
    363
    Russell Lear
    Jul 3, 2003
  3. Sankar
    Replies:
    12
    Views:
    459
    William Ahern
    Dec 6, 2006
  4. Sankar
    Replies:
    8
    Views:
    274
    CBFalconer
    Dec 5, 2006
  5. Shawn Mcclain
    Replies:
    0
    Views:
    190
    Shawn Mcclain
    Sep 28, 2007
Loading...

Share This Page