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

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

  1. 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

    Thanks'
    Rich
     
    , Oct 30, 2006
    #1
    1. Advertising

  2. Praveen Guest

    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, 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
     
    Praveen, Oct 30, 2006
    #2
    1. Advertising

  3. Jia Lu Guest

    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_mtime)

    2,
    os.path.getmtime()

    3, in Win32
    win32file.GetFileTime
    int = GetFileTime(handle, creationTime , accessTime , writeTime )
     
    Jia Lu, Oct 30, 2006
    #3
  4. 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>
     
    Fredrik Lundh, Oct 30, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rich Pasco
    Replies:
    2
    Views:
    19,461
    Rich Pasco
    Oct 9, 2003
  2. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    904
    Rob Schieber
    Sep 30, 2005
  3. Marc Mendez

    File last modified date wrong

    Marc Mendez, Aug 21, 2007, in forum: Java
    Replies:
    8
    Views:
    1,145
    Roedy Green
    Aug 23, 2007
  4. Elliot
    Replies:
    1
    Views:
    334
    siccolo
    Feb 27, 2008
  5. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    230
    Mark Schupp
    Aug 9, 2004
Loading...

Share This Page