473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Python - Sort files based on timestamp encoded in the filename

8
I have a list which contains list of file names, i wanted to sort list of those files based on timestamp encoded in file names.

Note: In file, Hello_Hi_2015-02-20T084521_1424543480.tar.gz --> 2015-02-20T084521 represents as "year-moth-dayTHHMMSS" ( Based on this i wanted to sort )

Input file below:

file_list = ['Hello_Hi_2015-02-20T084521_1424543480.tar.gz',
'Hello_Hi_2015-02-20T095845_1424543481.tar.gz',
'Hello_Hi_2015-02-20T095926_1424543481.tar.gz',
'Hello_Hi_2015-02-20T100025_1424543482.tar.gz',
'Hello_Hi_2015-02-20T111631_1424543483.tar.gz',
'Hello_Hi_2015-02-20T111718_1424543483.tar.gz',
'Hello_Hi_2015-02-20T112502_1424543483.tar.gz',
'Hello_Hi_2015-02-20T112633_1424543484.tar.gz',
'Hello_Hi_2015-02-20T113427_1424543484.tar.gz',
'Hello_Hi_2015-02-20T113456_1424543484.tar.gz',
'Hello_Hi_2015-02-20T113608_1424543484.tar.gz',
'Hello_Hi_2015-02-20T113659_1424543485.tar.gz',
'Hello_Hi_2015-02-20T113809_1424543485.tar.gz',
'Hello_Hi_2015-02-20T113901_1424543485.tar.gz',
'Hello_Hi_2015-02-20T113955_1424543485.tar.gz',
'Hello_Hi_2015-03-20T114122_1424543485.tar.gz',
'Hello_Hi_2015-02-20T114532_1424543486.tar.gz',
'Hello_Hi_2015-02-20T120045_1424543487.tar.gz',
'Hello_Hi_2015-02-20T120146_1424543487.tar.gz',
'Hello_WR_2015-02-20T084709_1424543480.tar.gz',
'Hello_WR_2015-02-20T113016_1424543486.tar.gz']

Output should be:

file_list = ['Hello_Hi_2015-02-20T084521_1424543480.tar.gz',
'Hello_WR_2015-02-20T084709_1424543480.tar.gz',
'Hello_Hi_2015-02-20T095845_1424543481.tar.gz',
'Hello_Hi_2015-02-20T095926_1424543481.tar.gz',
'Hello_Hi_2015-02-20T100025_1424543482.tar.gz',
'Hello_Hi_2015-02-20T111631_1424543483.tar.gz',
'Hello_Hi_2015-02-20T111718_1424543483.tar.gz',
'Hello_Hi_2015-02-20T112502_1424543483.tar.gz',
'Hello_Hi_2015-02-20T112633_1424543484.tar.gz',
'Hello_WR_2015-02-20T113016_1424543486.tar.gz',
'Hello_Hi_2015-02-20T113427_1424543484.tar.gz',
'Hello_Hi_2015-02-20T113456_1424543484.tar.gz',
'Hello_Hi_2015-02-20T113608_1424543484.tar.gz',
'Hello_Hi_2015-02-20T113659_1424543485.tar.gz',
'Hello_Hi_2015-02-20T113809_1424543485.tar.gz',
'Hello_Hi_2015-02-20T113901_1424543485.tar.gz',
'Hello_Hi_2015-02-20T113955_1424543485.tar.gz',
'Hello_Hi_2015-02-20T114532_1424543486.tar.gz',
'Hello_Hi_2015-02-20T120045_1424543487.tar.gz',
'Hello_Hi_2015-02-20T120146_1424543487.tar.gz',
'Hello_Hi_2015-03-20T114122_1424543485.tar.gz']
Below is the code which i have tried.
Expand|Select|Wrap|Line Numbers
  1. def sort( dir ):
  2.    os.chdir( dir )
  3.    file_list = glob.glob('Hello_*')
  4.    file_list.sort(key=os.path.getmtime)
  5.    print("\n".join(file_list))
  6.    return 0
  7.  
Thanks in advance!!
Aug 7 '15 #1

✓ answered by bvdet

Here is a regex solution:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. pattern = re.compile(r"^\D+?_\D+?_(.+?)_")
  4.  
  5. def sort_on_TS(a, b):
  6.     return cmp(pattern.match(a).group(1), pattern.match(b).group(1))
  7.  
  8. for item in sorted(file_list, sort_on_TS):
  9.     print item
This method uses string method split:
Expand|Select|Wrap|Line Numbers
  1. def sort_on_TS(a,b):
  2.     return cmp(a.split("_")[2], b.split("_")[2])
More lines to write, but you could also use string method index:
Expand|Select|Wrap|Line Numbers
  1. def sort_on_TS(a,b):
  2.     idx1 = a.index("_", a.index("_")+1)
  3.     idx2 = a.index("_", idx1+1)
  4.     idx11 = b.index("_", b.index("_")+1)
  5.     idx22 = b.index("_", idx11+1)
  6.     return cmp(a[idx1:idx2+1], b[idx11:idx22+1])

2 7595
bvdet
2,851 Expert Mod 2GB
Here is a regex solution:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. pattern = re.compile(r"^\D+?_\D+?_(.+?)_")
  4.  
  5. def sort_on_TS(a, b):
  6.     return cmp(pattern.match(a).group(1), pattern.match(b).group(1))
  7.  
  8. for item in sorted(file_list, sort_on_TS):
  9.     print item
This method uses string method split:
Expand|Select|Wrap|Line Numbers
  1. def sort_on_TS(a,b):
  2.     return cmp(a.split("_")[2], b.split("_")[2])
More lines to write, but you could also use string method index:
Expand|Select|Wrap|Line Numbers
  1. def sort_on_TS(a,b):
  2.     idx1 = a.index("_", a.index("_")+1)
  3.     idx2 = a.index("_", idx1+1)
  4.     idx11 = b.index("_", b.index("_")+1)
  5.     idx22 = b.index("_", idx11+1)
  6.     return cmp(a[idx1:idx2+1], b[idx11:idx22+1])
Aug 7 '15 #2
helloR
8
@bvdet: Excellent solution!!! Thank you very much!!!
Aug 8 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Ben | last post by:
Greetings, I am looking for a way to search for and delete files based on a pattern mask. For example, the search method would find all files matching a certain pattern containing wildcards (e.g....
3
by: fargo | last post by:
Hi. I'm looking for some way to sort files by date. I'm usin glob module to list a directiry, but files are sorted by name. >>> import glob >>> path = "./" >>> for ScannedFile in...
1
by: chris962 | last post by:
I have very basic knowledge of Javascript and am trying to get a web page to play one of two different sound files based on whether the user has correctly guessed a number. At the moment, all I...
1
by: Rups | last post by:
API FindFirstFile searches for files based on long and short names(8+3). If i need to search files based on long names only . How can i do ?Is there any other API which does search based on long...
17
by: Sunburned Surveyor | last post by:
I was thinking of a way I could make writing Python Class Files a little less painful. I was considering a Ptyhon script that read a file with a list of property names and method names and then...
4
by: jonathan184 | last post by:
Hi I have a perl script, basically what it is suppose to do is check a folder with files. Now the files are checked using a timestamp with the command ls -l so the timestamp in this format is...
3
by: pramodkh | last post by:
Hi All, I have a remote directory wherein some files will be generated. I am writting a PERL script in windows to get the latest file created in that directory.How do i achieve this? This is...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
1
by: kalia | last post by:
I have a text file and i want to Split the file into mulitple files based off the city and then create new files with the city name. I am able to read the file and also chnaged the semi colon to a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.