473,657 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding and copying files with python.

gtb
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.z ip
dx_ver_1.2.01.z ip
dx_ver_1.12.7.z ip
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 '07 #1
7 2189
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" <goodTweetieB.. .@hotmail.comwr ote:
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.z ip
dx_ver_1.2.01.z ip
dx_ver_1.12.7.z ip
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 '07 #2
On Apr 3, 8:21 am, "gtb" <goodTweetieB.. .@hotmail.comwr ote:
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.z ip
dx_ver_1.2.01.z ip
dx_ver_1.12.7.z ip
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 '07 #3
gtb
On Apr 3, 8:31 am, kyoso...@gmail. com wrote:
On Apr 3, 8:21 am, "gtb" <goodTweetieB.. .@hotmail.comwr ote:


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.z ip
dx_ver_1.2.01.z ip
dx_ver_1.12.7.z ip
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

Apr 3 '07 #4
On Apr 3, 8:47 am, "gtb" <goodTweetieB.. .@hotmail.comwr ote:
On Apr 3, 8:31 am, kyoso...@gmail. com wrote:
On Apr 3, 8:21 am, "gtb" <goodTweetieB.. .@hotmail.comwr ote:
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.z ip
dx_ver_1.2.01.z ip
dx_ver_1.12.7.z ip
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'pat htofiles\*.*.*. *.zip')

and then sort that.

Mike

Apr 3 '07 #5
gtb wrote:
On Apr 3, 8:31 am, kyoso...@gmail. com wrote:
>On Apr 3, 8:21 am, "gtb" <goodTweetieB.. .@hotmail.comwr ote:


>>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.N N.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

Apr 3 '07 #6
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.z ip
dx_ver_1.2.01.z ip
dx_ver_1.12.7.z ip
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.z ip"
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.listd ir('/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.z ip

Apr 3 '07 #7
gtb
On Apr 3, 9:42 am, "Daniele Varrazzo" <daniele.varra. ..@gmail.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
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.z ip
dx_ver_1.2.01.z ip
dx_ver_1.12.7.z ip
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.z ip"

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.listd ir('/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.z ip
Thanks all. Much to learn, but you have certainly helped me get
started.

jh

Apr 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2342
by: chris.atlee | last post by:
I'm writing a program in python that creates tar files of a certain maximum size (to fit onto CD/DVD). One of the problems I'm running into is that when using compression, it's pretty much impossible to determine if a file, once added to an archive, will cause the archive size to exceed the maximum size. I believe that to do this properly, you need to copy the state of tar file (basically the current file offset as well as the state of...
2
1289
by: Harlin Seritt | last post by:
Is there any way to fetch a website's host/version headers using Python? Thanks, Harlin
6
2066
by: Lalit | last post by:
I am new to python. Infact started yesterday and feeling out of place. I need to write a program which would transfer files under one folder structure (there are sub folders) to single folder. Can anyone give me some idea like which library files or commands would be suitable for this file transfer task. I am sure its matter of few commands. It would be even more great if I could get some sample code with instructions Thanks
13
10659
by: writeson | last post by:
Hi all, I'm writing some code that monitors a directory for the appearance of files from a workflow. When those files appear I write a command file to a device that tells the device how to process the file. The appearance of the command file triggers the device to grab the original file. My problem is I don't want to write the command file to the device until the original file from the workflow has been copied completely. Since these...
275
12247
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.