By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

trying to check the creation date of a file

P: n/a
People,

I am trying to determine the creation date of files in a folder.
I am using the following code to find the folder and confirm that files
exist in the folder. If someone could give me an idea how to check a
creation date it would be appreciated.
Thanks
dave

def delete_old_files (t:\dm\~\users)
# find files and delete files created more than XX number of days
ago
update_exist = 0

input_dir = t:\dm\~\users\xxxx.yyyyyy\zzzz

if os.path.exists (input_dir) :
files = os.listdir (input_dir)
else:
print "Unable to find input file dir: %s !!!" % input_dir
sys.exit(2)

if len(files):
for file in files :

file = os.path.join(input_dir) #all files in output
directory will be csv or xls, can be deleted if old enough

if os.path.isfile(file): #need to check ext not file, file
name changes each day
if re.search(t:\dm\~\users\xxxxx\) and #creation date
gt x number of days ago
t:\dm\~\users\davef.input_list.delete(file)

file_delete = 1

if file_delete:

print "\n file deleted: \n%s" %
str(t:\dm\~\users\xxxxx.input_list)

return file_delete

Dave Fickbohm
Data Mining Analyst
Homegain+
1250 45th St.
Emeryville, CA, 94608
Phone 510 594 4151 - Voice
510 655 0848 - Fax
Aug 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
use os.stat docs are here:
http://docs.python.org/lib/module-stat.html

Larry Bates

David Fickbohm wrote:
People,

I am trying to determine the creation date of files in a folder.
I am using the following code to find the folder and confirm that files
exist in the folder. If someone could give me an idea how to check a
creation date it would be appreciated.
Thanks
dave

def delete_old_files (t:\dm\~\users)
# find files and delete files created more than XX number of days
ago
update_exist = 0

input_dir = t:\dm\~\users\xxxx.yyyyyy\zzzz

if os.path.exists (input_dir) :
files = os.listdir (input_dir)
else:
print "Unable to find input file dir: %s !!!" % input_dir
sys.exit(2)

if len(files):
for file in files :

file = os.path.join(input_dir) #all files in output
directory will be csv or xls, can be deleted if old enough

if os.path.isfile(file): #need to check ext not file, file
name changes each day
if re.search(t:\dm\~\users\xxxxx\) and #creation date
gt x number of days ago
t:\dm\~\users\davef.input_list.delete(file)

file_delete = 1

if file_delete:

print "\n file deleted: \n%s" %
str(t:\dm\~\users\xxxxx.input_list)

return file_delete

Dave Fickbohm
Data Mining Analyst
Homegain+
1250 45th St.
Emeryville, CA, 94608
Phone 510 594 4151 - Voice
510 655 0848 - Fax


Aug 19 '05 #2

P: n/a
David Fickbohm wrote:
People,

I am trying to determine the creation date of files in a folder.
I am using the following code to find the folder and confirm that files
exist in the folder.
Presumably you meant "intend to use the following pseudocode" (not "am
using the following code") -- many of the statements (marked XXX below)
are just not valid Python.
If someone could give me an idea how to check a
creation date it would be appreciated.
Thanks
dave

def delete_old_files (t:\dm\~\users) XXX # find files and delete files created more than XX number of days
ago
update_exist = 0 Did you mean "file_delete = 0"?

input_dir = t:\dm\~\users\xxxx.yyyyyy\zzzz XXX
if os.path.exists (input_dir) :
files = os.listdir (input_dir)
else:
print "Unable to find input file dir: %s !!!" % input_dir
sys.exit(2)

if len(files): Not necessary -- "for file in files" does nothing gracefully if files is
empty. If it were necessary, "if not files:" is suggested as an
alternative to "if len(files)". for file in files :

file = os.path.join(input_dir) #all files in output
directory will be csv or xls, can be deleted if old enough
I think you mean "file = os.path.join(input_dir, file)"

if os.path.isfile(file): #need to check ext not file, file
name changes each day
if re.search(t:\dm\~\users\xxxxx\) and #creation date XXX
You don't need the re module to check if the file's extension is "csv"
or "xls"

gt x number of days ago
t:\dm\~\users\davef.input_list.delete(file) XXX
file_delete = 1

if file_delete:

print "\n file deleted: \n%s" %
str(t:\dm\~\users\xxxxx.input_list)

return file_delete


Now, to answer your question: You have obviously read the docs on the os
module; what did you not understand about os.stat()?

I suggest you contemplate the following real-not-pseudo-code and then
examine the relevant sections of the docs for os.stat(), the stat
module, and the time module.

C:\junk>type st_ctime.py
import os, stat, time

def get_create_time(path):
int_time = os.stat(path)[stat.ST_CTIME]
str_time = time.ctime(int_time)
return str_time

if __name__ == "__main__":
import glob, sys
for arg in sys.argv[1:]:
for path in glob.glob(arg):
create_time = get_create_time(path)
print path, create_time
C:\junk>st_ctime.py *c*.py
checkmodules.py Fri Jun 24 22:32:57 2005
ivancodecs.py Mon Jul 11 10:03:23 2005
st_ctime.py Sat Aug 20 09:22:00 2005

C:\junk>
Aug 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.