Daniel Paczuski Bak
Daniel Paczuski Bak

Reputation: 4088

Getting last modified for every file in a directory

I'm trying to get data for every file in a specific directory. Right now I'm just trying to get last-modified date. It seems like I need to convert this WindowsPath to a string, but I couldn't find any function that would do that.

import os
import time
from pathlib import Path

startDir = os.getcwd()

pt = r"\\folder1\folder2"

asm_pths = [pth for pth in Path(pt).iterdir()
            if pth.suffix == '.xml']


for file in asm_pths:
    (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
    print("last modified: %s" % time.ctime(mtime))

Console:

Traceback (most recent call last):
  File "C:\Users\daniel.bak\My Documents\LiClipse Workspace\Crystal Report Batch Analyzer\Analyzer\analyzer.py", line 34, in <module>
    (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
TypeError: argument should be string, bytes or integer, not WindowsPath

Upvotes: 15

Views: 14429

Answers (4)

Niko Fohr
Niko Fohr

Reputation: 34028

You may also use lstat() or stat() of a pathlib.Path object to get a stat_result object which holds information about the path/file. The last modified date can be calculated from the st_mtime, which is:

Time of most recent content modification expressed in seconds.

Example:

from pathlib import Path

file = Path(r'C:\Users\<user>\Desktop\file.txt')
file.lstat().st_mtime

Output: 1496134873.8279443

import datetime
datetime.datetime.fromtimestamp(file.lstat().st_mtime)

Output: datetime.datetime(2017, 5, 30, 12, 1, 13, 827944)

Upvotes: 21

kaki gadol
kaki gadol

Reputation: 1380

There is no need to use os.stat function, pathlib has the same function- file.stat() where file is you path object.
You can use the following code:

for file in asm_pths:
    (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = file.stat()
    print(f'last modified: {time.ctime(mtime)}')

But if you only want last modification date then use this:

for file in asm_pths:
    print(f'last modified: {time.ctime(file.stat().st_mtime)}')

I would prefer to avoid using os.path and pathlib.Path in the same project as much as possible in order to prevent confusion and bugs, because pathlib's paths are made of Path objects and os.path is expecting strings as paths.

Upvotes: 7

Sede
Sede

Reputation: 61293

The path argument to os.stat must be a string but you are passing in an instance of Path. You need to convert Path to string using str.

for file in asm_pths:
    (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(str(file))
    print("last modified: %s" % time.ctime(mtime))

But if you only want last modification date then os.path.getmtime will be fine:

for file in asm_pths:
    print("last modified: %s" % time.ctime(os.path.getmtime(str(file)))

Upvotes: 4

knh170
knh170

Reputation: 2990

os.path.getmtime(file) should give you the anwser. Your problem is that file type should be a string. Change your code to something like:

# a list of string
paths = [f for f in os.listdir(".") if f.endswith(".xml")]
for f in paths:
    print("last modified: %s" % time.ctime(os.path.getmtime(f)))

Upvotes: 4

Related Questions