473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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

Oct 30 '06 #1
3 7141
I hope this sample code helps

def getfileinfo(filename):
print 'Filename : %s' % filename
stats = os.stat(filename)
size = stats[stat.ST_SIZE]
print 'File Size is %d bytes' % size

accessed = stats[stat.ST_ATIME]
modified = stats[stat.ST_MTIME]

print 'Last accessed: ' + time.ctime(accessed)
print 'Last modified: ' + time.ctime(modified)

Regards,
Praveen

On Oct 30, 8:00 am, RAMohrm...@adelphia.net 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
Oct 30 '06 #2

RA********@adelphia.net 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.
For this you have some solutions.

1,
import os
import time
time.ctime(os.stat(r"L:\MyDoc\EBook\Python").st_mt ime)

2,
os.path.getmtime()

3, in Win32
win32file.GetFileTime
int = GetFileTime(handle, creationTime , accessTime , writeTime )

Oct 30 '06 #3
RA********@adelphia.net 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):

import glob, os, time

now = time.time()

for file in glob.glob("c:/informatics/cache/*"):
age = os.path.gettime(file) - now
print file, "is", age / 3600, "hours old"

adding code to remove old files should be straightforward.

</F>

Oct 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Shawn McKenzie | last post by:
Can someone help me with a query to get the date/time that a database was last modified? I looked at the SHOW command but there was no mention of this. TIA, Shawn
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
1
by: BW | last post by:
I am creating an upload/download function for an extranet site. Files will be uploaded to directory based upon the users login and associated project. The function works as long as I use "c:\Temp"...
3
by: Shailesh Humbad | last post by:
I figured out what was causing the "Access is Denied" error when calling functions from referenced DLLs in my service. I've tried to be very detailed, so bear with me. It turns out that...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
2
by: LisaB | last post by:
I am new to Visual Basic .Net and would like to build a simple application that searches a directory and returns the Path, Size and DateModified of all files that end in ".MDB". a) I would...
9
by: Fish Womper | last post by:
I am at best a part time developer of Access databases. I use Access 2.0, as this is all my employer has on its computers. Even so, to use this ancient version requires a fairly convoluted...
5
by: techusky | last post by:
I made a script that successfully creates a .zip file of all the files in a directory on my web server, but now what I haven't figured out how to do is how to have it automatically deleted when the...
3
by: Bouzy | last post by:
I am trying to make a script to check files in a folder, then see later if the files have been changed at all.. I have this working to make the original file (data log with files,file_sizes, and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.