Finding and copying files with python.
steve at holdenweb.com
Tue Apr 3 16:07:44 CEST 2007
> On Apr 3, 8:31 am, kyoso... at gmail.com wrote:
>> On Apr 3, 8:21 am, "gtb" <goodTweetieB... at hotmail.com> 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
>>> filename_MM.NN.SS.zip, where MM, NN, and SS can be one to three
>>> Example directory:
>>> 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.
>> 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.
> 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
> Thanks again,
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.
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
More information about the Python-list