Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: read dir contents and print them

  1. #11
    Join Date
    Mar 2005
    Beans
    554

    Re: read dir contents and print them

    Quote Originally Posted by ghostdog74 View Post
    what actually are you trying to do? the file dates you mentioned, do you want to find last modified date? creation date? or accessed date? pls check out the os module i mentioned. you should be able to get the timestamp of the files in seconds. then you can store them in lists and use the sorted() method or sort() to sort the list...
    Code:
    print sorted([ (os.path.getmtime(files),files) for files in os.listdir(os.getcwd()) ])
    Im trying to get the script to get the dir listing, evaluate all of the folders in that dir and print only the dirs that are 1 week old or less, 1 per line.

    So far, here is the code I am working with:

    Code:
    #!/usr/bin/env python
    
    import dircache
    a=dircache.listdir('/mnt/fileserver/uploads')
    for s in a:
            print s
    nix4me

  2. #12
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: read dir contents and print them

    in C (sorry, it's the only way I know how to do it), there is opendir() which opens the directory file and reads the entries, then there is stat() which actually reads the information about a file. you need the equivalent of stat

  3. #13
    Join Date
    Sep 2006
    Beans
    2,914

    Re: read dir contents and print them

    Quote Originally Posted by nix4me View Post
    Im trying to get the script to get the dir listing, evaluate all of the folders in that dir and print only the dirs that are 1 week old or less, 1 per line.
    i have showed you a snippet of how it can be done.
    use os.path.getmtime (or ctime, or atime depending on your requirements).
    eg
    Code:
    >>> import time,os
    >>> today=time.time()
    >>> filetime = os.path.getmtime("file")
    1179703606
    you get seconds. 1 week is roughly equivalent to 24 * 60 * 60 * 7 =
    604800 seconds, so you subtract today's time with 604800 and that is the time
    one week ago.Store it in a variable then in your script you can check whether filetime is less than one week and
    print it.

  4. #14
    Join Date
    Nov 2006
    Beans
    Hidden!

    Re: read dir contents and print them

    Code:
    import os
    import time
    for file in [file for file in os.listdir(os.curdir) if time.time()-os.path.getmtime(file) < 604800]:
    	print file

  5. #15
    Join Date
    Mar 2005
    Beans
    554

    Re: read dir contents and print them

    Perfect. Thanks so much, i was having trouble getting the date.date portion working. It's working great. I've managed to tweak it accordingly to work with the individual directories that i want.

    Thanks again,
    nix4me

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •