How to access file last modified dates on each file in a directory

Discussion in 'Python' started by RAMohrmann@adelphia.net, Oct 30, 2006.

Guest

Greetings,

I am attempting to view all files in a directory and if those files
have not been modified within the last couple days I will remove them.
In order to do this I need to look at the file date modied and check
the date. I know how to look at each file name and I know how to remove
the file. I just can't figure out how to get access to the date last
modifed filed. Below is how I intend to access the file names in the
directory.
>>> import os,time,sys
>>> cachedirectory="c:\\informatica\\cache\\"
>>> v_filename_array=os.listdir(cachedirectory)
>>> x_len=len(v_filename_array)

v_filename_array[0] = first file name
v_filename_array[1] - second file name

On Oct 30, 8:00 am, wrote:
> Greetings,
>
> I am attempting to view all files in a directory and if those files
> have not been modified within the last couple days I will remove them.
> In order to do this I need to look at the file date modied and check
> the date. I know how to look at each file name and I know how to remove
> the file. I just can't figure out how to get access to the date last
> modifed filed. Below is how I intend to access the file names in the
> directory.
>
> >>> import os,time,sys
> >>> cachedirectory="c:\\informatica\\cache\\"
> >>> v_filename_array=os.listdir(cachedirectory)
> >>> x_len=len(v_filename_array)v_filename_array[0] = first file name
> v_filename_array[1] - second file name
>
> Thanks'
> Rich

Advertisements

wrote:
> Greetings,
>
> I am attempting to view all files in a directory and if those files
> have not been modified within the last couple days I will remove them.
> In order to do this I need to look at the file date modied and check
> the date. I know how to look at each file name and I know how to remove
> the file. I just can't figure out how to get access to the date last
> modifed filed.

wrote:
> I am attempting to view all files in a directory and if those files
> have not been modified within the last couple days I will remove them.
> In order to do this I need to look at the file date modied and check
> the date. I know how to look at each file name and I know how to remove
> the file. I just can't figure out how to get access to the date last
> modifed filed. Below is how I intend to access the file names in the
> directory.
>
>>>> import os,time,sys
>>>> cachedirectory="c:\\informatica\\cache\\"
>>>> v_filename_array=os.listdir(cachedirectory)

since listdir only returns the last part of the full file path, it's
often easier to use glob.

for file in glob.glob("c:/informatics/cache/*"):
...

otherwise, you need to do os.path.join(cachedirectory, file) for each
file in the filename list, to get a full path.

to get the age of a file, use os.path.getmtime(filename). this returns
the modification time as seconds since a reference time (usually called
the "epoch". if you subtract this time from the current time, you get
the age (in seconds):

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!