Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: read dir contents and print them

  1. #1
    Join Date
    Mar 2005
    Beans
    554

    read dir contents and print them

    I'm trying to find a way to make a script that reads the contents of a system dir and prints the contents.

    Secondly I would like it to only list the contents of new dirs based on a set dir creation date. Such as, if i want to see all dirs in a path that have been created in the last 5 days.

    It looks like I might be able to use the dircache.listdir() function but i'm not having any luck.

    nix4me

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

    Re: read dir contents and print them

    Quote Originally Posted by nix4me View Post
    I'm trying to find a way to make a script that reads the contents of a system dir and prints the contents.
    you are using Python i assume? and have you started to code yet?
    some of the methods you can use from the os module is listdir, walk() or even glob for wildcard pattern matching... pls have a look at the docs

    Secondly I would like it to only list the contents of new dirs based on a set dir creation date. Such as, if i want to see all dirs in a path that have been created in the last 5 days.
    you can make use of getctime/getatime/getmtime from os.path module . also related is the time module, stat() from os module .( pls see the docs for more details )


    It looks like I might be able to use the dircache.listdir() function but i'm not having any luck.
    you had errors?

  3. #3
    Join Date
    Mar 2005
    Beans
    554

    Re: read dir contents and print them

    Well, this is a start:

    >>> import dircache
    >>> a=dircache.listdir('/mnt/fileserver/uploads')
    >>> a=a[:]
    >>> a

    This prints all of the directories in one long line. I would like to break this into 1 dir per line.

    Then I will reasearch how to only get certain dirs based on date.

    I am using python and I am mostly new to programming, however I have been able to hack things together pretty quickly. Python is very nice, easy to learn.

    Any help would be greatly appreciated.

    nix4me

  4. #4
    Join Date
    Apr 2007
    Beans
    69

    Re: read dir contents and print them

    Code:
    >>> a=a[:]
    That is not needed. You already have a and don't need to put the contents of it back into it.

    Code:
    >>> a
    a is an array of all the directories. Above you are just dumping this list. To print it on one line you need to loop though the array and print each item in it. Here is an example of how to do this:
    Code:
    #!/usr/bin/env python
    
    import dircache
    a=dircache.listdir('/mnt/fileserver/uploads')
    for s in a:
            print s

  5. #5
    Join Date
    Mar 2005
    Beans
    554

    Re: read dir contents and print them

    Excellent, that works nicely.

    Now off to figure out how to shorten the list based on file dates.

    nix4me

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

    Re: read dir contents and print them

    umm, if I was using perl, I'd get output of ls -la and then parse the output ...

    example:
    @dirs = `ls -la /some/dir/`; #array of lines
    for(@dirs) { # for each line of output
    @fields = split /\s/, $_; #split the line on spaces
    #there are function to convert time and such, and you need only 1 field in the @fields array
    }

    the pseudo code is there, just finish it

  7. #7
    Join Date
    Sep 2006
    Beans
    2,914

    Re: read dir contents and print them

    Quote Originally Posted by slavik View Post
    umm, if I was using perl, I'd get output of ls -la and then parse the output ...

    example:
    @dirs = `ls -la /some/dir/`; #array of lines
    for(@dirs) { # for each line of output
    @fields = split /\s/, $_; #split the line on spaces
    #there are function to convert time and such, and you need only 1 field in the @fields array
    }

    the pseudo code is there, just finish it
    IMO, that's not a "portable" way to code as you are shelling out from perl to call an OS specific command. there is a readdir you can use.

  8. #8
    Join Date
    Mar 2005
    Beans
    554

    Re: read dir contents and print them

    Well i appreciate the perl response, but i am trying to do it in python. So far, i have not figured out a way to sort/split based on the file dates.

    nix4me

  9. #9
    Join Date
    Sep 2006
    Beans
    2,914

    Re: read dir contents and print them

    Quote Originally Posted by nix4me View Post
    .. sort/split based on the file dates.
    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()) ])

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

    Re: read dir contents and print them

    Quote Originally Posted by ghostdog74 View Post
    IMO, that's not a "portable" way to code as you are shelling out from perl to call an OS specific command. there is a readdir you can use.
    platform independent was not in OP

Page 1 of 2 12 LastLast

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
  •