473,399 Members | 2,858 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,399 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 7601
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
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
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.