Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read file attributes in a directory?

Tags:

python

file

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?

like image 927
Bdfy Avatar asked Jun 09 '12 11:06

Bdfy


People also ask

How can I see the attributes of a file?

View or change file attributes To view or change the attributes of a file, right-click the file, and then click Properties. In the "Attributes:" section, enabled attributes have checks beside them. Add or remove the checks from Read-only, Archive, or Hidden to enable or disable these options.

How can the attributes of files in a directory be displayed?

The hidden file attribute hides files and folders from the directory. To apply this attribute, right-click a file and select Properties > Hidden. To view all files, including hidden files, from the Command Prompt in Windows, use this dir command option: dir /a.

How do I see file attributes in Linux?

You can list the attribute of the contents of a particular directory with lsattr command followed with a file or directory name as the argument. As the ls -l command, the -d option with lsattr will list the attributes of the directory itself instead of the files in that directory.


1 Answers

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.

like image 126
Martijn Pieters Avatar answered Oct 31 '22 21:10

Martijn Pieters