trying to check the creation date of a file

John Machin sjmachin at lexicon.net
Fri Aug 19 19:53:32 EDT 2005


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>



More information about the Python-list mailing list