473,750 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
cachedirector y="c:\\informat ica\\cache\\"
v_filename_ar ray=os.listdir( cachedirectory)
x_len=len(v_f ilename_array)
v_filename_arra y[0] = first file name
v_filename_arra y[1] - second file name

Thanks'
Rich

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

def getfileinfo(fil ename):
print 'Filename : %s' % filename
stats = os.stat(filenam e)
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(acce ssed)
print 'Last modified: ' + time.ctime(modi fied)

Regards,
Praveen

On Oct 30, 8:00 am, RAMohrm...@adel phia.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:\\informati ca\\cache\\"
v_filename_arr ay=os.listdir(c achedirectory)
x_len=len(v_fi lename_array)v_ filename_array[0] = first file name
v_filename_arra y[1] - second file name

Thanks'
Rich
Oct 30 '06 #2

RA********@adel phia.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.s tat(r"L:\MyDoc\ EBook\Python"). st_mtime)

2,
os.path.getmtim e()

3, in Win32
win32file.GetFi leTime
int = GetFileTime(han dle, creationTime , accessTime , writeTime )

Oct 30 '06 #3
RA********@adel phia.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
cachedirecto ry="c:\\informa tica\\cache\\"
v_filename_a rray=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(ca chedirectory, file) for each
file in the filename list, to get a full path.

to get the age of a file, use os.path.getmtim e(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
1082
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
24035
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 tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
1
2763
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" as the directory. When I use any other hard coded directory or even Server.MapPath() the upload function fails and returns the error: "Exception has been thrown by the target of an invocation." Once I change the root directory to "c:\Temp",...
3
8006
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 libraries I made myself were not having this problem, but it only happened with a zip library I downloaded and copied into my program's directory. When using classes from this library, my service threw an "Access Is Denied" FileLoadException, which...
0
2570
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 datalist. When the next user selects trys to run the page, the page fails and I get a generic error message from the stack trace. I am assuming that the document properties cannot be read when a file is open, but it worked well in asp. ...
2
6910
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 create a form to allow the user to enter the directory to be searched - EX.. "C:\" or "D:\Program FIles" would be entered by the user ------This is the part I need Help with---------- b) The form would have a button with the following on_Click...
9
2530
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 installation procedure on each PC on which it is used. I am self-taught from the help files that come with Access 2.0 and from painful experience. I've never attended any type of training course on how to develop databases in Access. I have no idea...
5
2227
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 user successfully downloads it, as otherwise my server would eventually get clogged up with all these zip files. Any help/suggestions? Thanks
3
2286
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 dates). log_file = open('log_file.txt', 'w') for root,dirs,files in os.walk(cwd): for folder in glob.glob(root): for file in glob.glob(folder + '/*.**'): file_path = os.path.split(file)
0
8836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.