Frage

I was trying to get the name, date last accessed and date last modified from files in a folder and everything worked. Now i tried to add the filesize, but from there I got this error.

My working result without size:

[['28e20ee3-8e8c-427d-af73-cd9de58b5811_1156153318066-Ajax_Thuis_A4_665.jpg', 'Fri Oct 18 10:28:11 2013', 'Fri Oct 18 10:28:11 2013']]

My code looks like this

import os,time

def get_information(directory):
    file_list = []
    for i in os.listdir(directory):
        a = os.stat(os.path.join(directory,i))
        file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
    return file_list

print get_information("/home/randy/testfolder")

My code with size looks like this (relevant only):

file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime),os.path.getsize(a)])

How do i get the output to show the filesize without errors? I need to do this on loads of files.

War es hilfreich?

Lösung

You have

a = os.stat(os.path.join(directory,i))
os.path.getsize(a)

os.path.getsize takes a path as argument but you give it the result of os.stat.

You can use a.st_size directly instead of calling os.path.getsize

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top