Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Lists Using Lists Split and Join

Hayley Risley
Hayley Risley
9,510 Points

how do you create a python script that collects file name, size, and date created

I am trying to create a simple python application that takes two parameters and collects all the file names, size, and date created in a given folder. I have looked on Google and browsed online tutorials and I'm still not sure how to go about this. Any and all help/guidance is appreciated.

Steven Parker
Steven Parker
229,744 Points

It's not clear what the relationship is between the "two parameters" and the "file name, size, and date". But in any case, it would be helpful to post your current code, preferably as a link to a workspace snapshot.

2 Answers

Hi Hayley,

I started reading the os module and path documentation as I wanted to give this a try myself.

I came across the attributes relating to size etc....

It was a bit of trial and error below is some code that might of of use for creating your function.

I find reading the documentation first is better than turning straight to stack overflow, tutorials etc...

If I go to stack overflow it just too much information and often leads me down a rabbit hole.

import os
from path import Path
import datetime
import time

# check the current directory
cwd = os.getcwd()

# change current directory
cwd = Path('C:/Users/name/Documents/test')

for file in cwd.files():
    file_name = file
    last_Mod = os.stat(file).st_ctime
    size = os.stat(file).st_size
    print(file_name)
    print(size)
    print(datetime.datetime.strptime(time.ctime(last_Mod), "%a %b %d %H:%M:%S %Y"))

I found this helpful as never done it before.

Hi, where would you read the documentation for python. could you send me a link

Steven Parker
Steven Parker
229,744 Points

Try the online Python Language Reference.

But next time, start a new question instead of posting one as a comment to an old question.

I got an error

line 2, in <module> from path import Path ModuleNotFoundError: No module named 'path'

Hi Moath, think ran this in the terminal so that why it worked for me.

If you change the line to that should work.

from pathlib import Path