Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:46:16+00:00 2026-06-05T23:46:16+00:00

For example: import os print(os.listdir("path/to/dir")) will list files in a directory. How do I

  • 0

For example:

import os
print(os.listdir("path/to/dir"))

will list files in a directory.

How do I get the file modification time for all files in the directory?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:46:18+00:00Added an answer on June 5, 2026 at 11:46 pm

    When looking for file attributes for all files in a directory, and you are using Python 3.5 or newer, use the os.scandir() function to get a directory listing with file attributes combined. This can potentially be more efficient than using os.listdir() and then retrieve the file attributes separately:

    import os
    
    with os.scandir() as dir_entries:
        for entry in dir_entries:
            info = entry.stat()
            print(info.st_mtime)
    

    The DirEntry.stat() function, when used on Windows, doesn’t have to make any additional system calls, the file modification time is already available. The data is cached, so additional entry.stat() calls won’t make additional system calls.

    You can also use the pathlib module Object Oriented instances to achieve the same:

    from pathlib import Path
    
    for path in Path('.').iterdir():
        info = path.stat()
        print(info.st_mtime)
    

    On earlier Python versions, you can use the os.stat call for obtaining file properties like the modification time.

    import os
    
    for filename in os.listdir():
        info = os.stat(filename)
        print(info.st_mtime)
    

    st_mtime is a float value on python 2.5 and up, representing seconds since the epoch; use the time or datetime modules to interpret these for display purposes or similar.

    Do note that the value’s precision depends on the OS you are using:

    The exact meaning and resolution of the st_atime, st_mtime, and st_ctime attributes depend on the operating system and the file system. For example, on Windows systems using the FAT or FAT32 file systems, st_mtime has 2-second resolution, and st_atime has only 1-day resolution. See your operating system documentation for details.

    If all you are doing is get the modification time, then the os.path.getmtime method is a handy shortcut; it uses the os.stat method under the hood.

    Note however, that the os.stat call is relatively expensive (file system access), so if you do this on a lot of files, and you need more than one datapoint per file, you are better off using os.stat and reuse the information returned rather than using the os.path convenience methods where os.stat will be called multiple times per file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an example that searches PDF files in the current directory. import os,
I need to save output in Python. Example: import os ob_start(); os.system( 'c:\files\file.exe' );
Take the following code example: File package1/__init__.py : from moduleB import foo print moduleB.__name__
Consider the following example: import string,cgi,time from os import curdir, sep from BaseHTTPServer import
Example: import numpy print numpy.polydiv.__doc__ output: Returns the quotient and remainder of polynomial division...
Example: import user class Thing(object): def doSomething(self): u = user.User(1) print u.name >> UnboundLocalError:
import os print __file__ print os.path.dirname(__file__) os.chdir('/tmp') print __file__ # unchanged, of course print
I couldn't find a more descriptive title, but here there is an example: import
I'd like to initialize some top-level constants using float::consts::pi . For example: import float::consts::pi;
import java.util.Collection; import example.Event; public interface Query { public boolean hasMore (); public Collection<Event>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.