Hướng dẫn how to sort files based on timestamp in python - cách sắp xếp tệp dựa trên dấu thời gian trong python

Tôi có một danh sách chứa danh sách các tên tệp, tôi muốn sắp xếp dựa trên dấu thời gian, mà (tức là dấu thời gian) được xây dựng trong mỗi tên tệp.

Lưu ý: Trong tập tin, Hello_HI_2015-02-20T084521_1424543480.TAR.GZ-> 2015-02-20T084521 đại diện cho "Ngày nay-Daythhmms" (dựa trên điều này tôi muốn sắp xếp)

Tệp đầu vào bên dưới:

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']  

Đầu ra phải là:

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']  

Dưới đây là mã mà tôi đã thử.

def sort( dir ):
   os.chdir( dir )
   file_list = glob.glob('Hello_*')
   file_list.sort(key=os.path.getmtime)
   print("\n".join(file_list))
   return 0

Cảm ơn trước!!

Trong bài viết này, chúng tôi sẽ thảo luận về các cách khác nhau để có được danh sách tất cả các tệp trong thư mục / thư mục được sắp xếp theo ngày và thời gian trong Python.

Mục lục

  • Nhận danh sách các tệp (đường dẫn tệp) trong thư mục được sắp xếp theo ngày.
  • Nhận danh sách các tên tệp trong thư mục được sắp xếp theo ngày.
  • Nhận danh sách các tệp (đường dẫn tệp) trong thư mục và các thư mục phụ được sắp xếp theo ngày.

Trong Python, mô -đun GLOB cung cấp một hàm hàm () để tìm tệp trong một thư mục dựa trên mẫu phù hợp. Tương tự như các quy tắc mở rộng đường dẫn Unix, chúng ta có thể sử dụng các ký tự đại diện và biểu thức thông thường để khớp và tìm một vài hoặc tất cả các tệp/thư mục phụ trong một thư mục. Chúng tôi sẽ sử dụng điều này để có được danh sách tất cả các tệp trong một thư mục nhưng được sắp xếp theo thời gian sửa đổi cuối cùng. Các bước như sau,glob() to find files in a directory based on matching pattern. Similar to the unix path expansion rules, we can use wildcards and regular expression to match & find few or all files/sub-directories in a directory. We will use this to get a list of all files in a directory but sorted by the last modification time. Steps are as follows,

  1. Nhận một danh sách tất cả các tệp & thư mục trong thư mục đã cho bằng GLOB ().
  2. Sử dụng hàm Filter () và OS.Path.isfileio (), chỉ chọn các tệp từ danh sách. filter() function and os.path.isfileIO(), select files only from the list.
  3. Sắp xếp danh sách các tệp dựa trên thời gian sửa đổi cuối cùng bằng cách sử dụng hàm sort ().
    • Đối với điều này, hãy sử dụng Os.Path.GetMtime () làm đối số chính trong hàm Sắp xếp ().os.path.getmtime() as the key argument in the sorted() function.

Hoàn thành ví dụ để có được danh sách tất cả các tệp trong thư mục được sắp xếp theo DateTime sửa đổi cuối cùng như sau,

import glob
import os
import time


dir_name = 'C:/Program Files/Java/jdk1.8.0_191/'

# Get list of all files only in the given directory
list_of_files = filter( os.path.isfile,
                        glob.glob(dir_name + '*') )

# Sort list of files based on last modification time in ascending order
list_of_files = sorted( list_of_files,
                        key = os.path.getmtime)

# Iterate over sorted list of files and print file path 
# along with last modification time of file 
for file_path in list_of_files:
    timestamp_str = time.strftime(  '%m/%d/%Y :: %H:%M:%S',
                                time.gmtime(os.path.getmtime(file_path))) 
    print(timestamp_str, ' -->', file_path) 

Output:

Quảng cáo

10/06/2018 :: 04:34:06  --> C:/Program Files/Java/jdk1.8.0_191\COPYRIGHT      
10/06/2018 :: 04:34:08  --> C:/Program Files/Java/jdk1.8.0_191\src.zip        
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\LICENSE        
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\README.html    
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\THIRDPARTYLICENSEREADME-JAVAFX.txt
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\THIRDPARTYLICENSEREADME.txt
11/18/2018 :: 09:42:12  --> C:/Program Files/Java/jdk1.8.0_191\javafx-src.zip 
11/18/2018 :: 09:42:19  --> C:/Program Files/Java/jdk1.8.0_191\release  

Trong giải pháp này, chúng tôi đã tạo một danh sách các tệp trong một thư mục, được sắp xếp theo ngày. Nhưng danh sách chứa đường dẫn hoàn chỉnh của các tệp. Điều gì sẽ xảy ra nếu chúng ta chỉ muốn tên tệp theo thứ tự được sắp xếp theo ngày và giờ?

Nhận danh sách các tệp trong thư mục được sắp xếp theo ngày bằng os.listdir ()

Trong Python, mô -đun HĐH cung cấp chức năng ListDir (DIR_PATH), trả về danh sách các tên tệp & thư mục trong đường dẫn thư mục đã cho. Sử dụng hàm Filter () và OS.Path.isfileio (), chỉ chọn các tệp từ danh sách. Sau đó, chúng ta có thể sắp xếp danh sách tên tệp này dựa trên thời gian sửa đổi cuối cùng, sử dụng hàm Os.Path.getMtime () làm đối số chính trong hàm Sắp xếp ().os module provides a function listdir(dir_path), which returns a list of file & directory names in the given directory path. Using the filter() function and os.path.isfileIO(), select files only from the list. Then we can sort this list of file names based on the last modification time, using the os.path.getmtime() function as the key argument in the sorted() function.

Hoàn thành ví dụ để lấy danh sách các tệp trong thư mục được sắp xếp theo DateTime sửa đổi cuối cùng như sau,

import os
import time


dir_name = 'C:/Program Files/Java/jdk1.8.0_191/'

# Get list of all files only in the given directory
list_of_files = filter( lambda x: os.path.isfile(os.path.join(dir_name, x)),
                        os.listdir(dir_name) )

# Sort list of files based on last modification time in ascending order
list_of_files = sorted( list_of_files,
                        key = lambda x: os.path.getmtime(os.path.join(dir_name, x))
                        )

# Iterate over sorted list of files and print file path 
# along with last modification time of file 
for file_name in list_of_files:
    file_path = os.path.join(dir_name, file_name)
    timestamp_str = time.strftime(  '%m/%d/%Y :: %H:%M:%S',
                                time.gmtime(os.path.getmtime(file_path))) 
    print(timestamp_str, ' -->', file_name)    

Output:

10/06/2018 :: 04:34:06  --> COPYRIGHT
10/06/2018 :: 04:34:08  --> src.zip
11/18/2018 :: 09:42:11  --> LICENSE
11/18/2018 :: 09:42:11  --> README.html
11/18/2018 :: 09:42:11  --> THIRDPARTYLICENSEREADME-JAVAFX.txt
11/18/2018 :: 09:42:11  --> THIRDPARTYLICENSEREADME.txt
11/18/2018 :: 09:42:12  --> javafx-src.zip
11/18/2018 :: 09:42:19  --> release

Trong giải pháp này, chúng tôi đã tạo một danh sách các tên tệp trong một thư mục được sắp xếp theo ngày. Hàm Sắp xếp () sử dụng đối số chính làm bộ so sánh trong khi sắp xếp các mục trong danh sách đã cho. Do đó, bằng cách chuyển os.path.getmtime () làm đối số chính, chúng tôi đã buộc nó phải sắp xếp các tệp theo thời gian sửa đổi cuối cùng,os.path.getmtime() as the key argument, we forced it to sort the files by last modification time,

Python: Nhận danh sách các tệp trong thư mục và các thư mục phụ được sắp xếp theo ngày

Trong cả hai ví dụ trước, chúng tôi đã tạo một danh sách các tệp trong một thư mục được sắp xếp theo ngày. Nhưng nó chỉ bao gồm các tệp trong thư mục đã cho, không phải trong các thư mục lồng nhau. Vì vậy, nếu bạn muốn có một danh sách các tệp trong thư mục và thư mục phụ được sắp xếp theo ngày thì hãy kiểm tra ví dụ này,

import glob
import os
import time


dir_name = 'C:/Program Files/Java/jdk1.8.0_191/'

# Get list of all files only in the given directory
list_of_files = filter( os.path.isfile,
                        glob.glob(dir_name + '/**/*') )

# Sort list of files based on last modification time in ascending order
list_of_files = sorted( list_of_files,
                        key = os.path.getmtime)

# Iterate over sorted list of files and print file path 
# along with last modification date time 
for file_path in list_of_files:
    timestamp_str = time.strftime(  '%m/%d/%Y :: %H:%M:%S',
                                time.gmtime(os.path.getmtime(file_path))) 
    print(timestamp_str, ' -->', file_path)  

Output:

11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\bin\appletviewer.exe
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\bin\extcheck.exe      
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\bin\idlj.exe
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\include\jdwpTransport.h
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\include\jni.h
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\include\jvmti.h
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\include\jvmticmlr.h
11/18/2018 :: 09:42:13  --> C:/Program Files/Java/jdk1.8.0_191\jre\COPYRIGHT
11/18/2018 :: 09:42:13  --> C:/Program Files/Java/jdk1.8.0_191\jre\Welcome.html
11/18/2018 :: 09:42:13  --> C:/Program Files/Java/jdk1.8.0_191\lib\ant-javafx.jar
11/18/2018 :: 09:42:14  --> C:/Program Files/Java/jdk1.8.0_191\lib\ct.sym
11/18/2018 :: 09:42:14  --> C:/Program Files/Java/jdk1.8.0_191\lib\dt.jar
11/18/2018 :: 09:42:14  --> C:/Program Files/Java/jdk1.8.0_191\lib\jvm.lib
11/18/2018 :: 09:42:17  --> C:/Program Files/Java/jdk1.8.0_191\lib\orb.idl
11/18/2018 :: 09:42:17  --> C:/Program Files/Java/jdk1.8.0_191\lib\packager.jar
11/18/2018 :: 09:42:17  --> C:/Program Files/Java/jdk1.8.0_191\lib\sa-jdi.jar
11/18/2018 :: 09:42:26  --> C:/Program Files/Java/jdk1.8.0_191\lib\tools.jar
11/18/2018 :: 09:42:26  --> C:/Program Files/Java/jdk1.8.0_191\jre\lib\plugin.jar
11/18/2018 :: 09:42:27  --> C:/Program Files/Java/jdk1.8.0_191\jre\lib\javaws.jar
11/18/2018 :: 09:42:27  --> C:/Program Files/Java/jdk1.8.0_191\jre\lib\deploy.jar
11/18/2018 :: 09:42:31  --> C:/Program Files/Java/jdk1.8.0_191\jre\lib\rt.jar
11/18/2018 :: 09:42:32  --> C:/Program Files/Java/jdk1.8.0_191\jre\lib\jsse.jar
11/18/2018 :: 09:42:32  --> C:/Program Files/Java/jdk1.8.0_191\jre\lib\charsets.jar
11/18/2018 :: 09:42:32  --> C:/Program Files/Java/jdk1.8.0_191\jre\lib\ext\localedata.jar
11/18/2018 :: 09:42:34  --> C:/Program Files/Java/jdk1.8.0_191\jre\bin\server\classes.jsa

Chúng tôi đã sử dụng hàm glob () với mẫu ‘/**/*, và đệ quy = đối số thật. Nó đã đưa ra một danh sách tất cả các tệp trong thư mục đã cho và tất cả các hướng dẫn phụ. Sau đó, sử dụng hàm Os.Path.getMtime () làm đối số chính trong hàm Sắp xếp (), chúng tôi đã tạo một danh sách các tệp được sắp xếp theo ngày và giờ.‘/**/*’ and recursive=True argument. It gave a list of all files in the given directory and all sub-directories. Then using the os.path.getmtime() function as the key argument in sorted() function, we created a list of files sorted by date and time.

Summary:

Chúng tôi đã tìm hiểu về các cách khác nhau để có được một danh sách các tệp trong một thư mục, được sắp xếp theo ngày & giờ.