Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Reading all of the files in a directory into a list

ITAITA Posts: 463Member
I've got a program which will eventually need to play a load of images from a directory in sequence for an animation.

What I want to do is to read the filenames of all these images from the directory into a list so that I can then iterate over the list with something like [code]for x in thelistoffilenames. [/code]

What's the best way to do this?

Please help, many thanks.

ITA

"Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

Comments

  • ITAITA Posts: 463Member
    : I've got a program which will eventually need to play a load of images from a directory in sequence for an animation.
    :
    : What I want to do is to read the filenames of all these images from the directory into a list so that I can then iterate over the list with something like [code]for x in thelistoffilenames. [/code]
    :
    : What's the best way to do this?
    :
    : Please help, many thanks.
    :
    : ITA
    :
    : "Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"
    :

    Sorry, I've been foolish once again - I should have read the rest (or at least searched) the rest of the forum to find the answer before I posted!

    Looking further down, I found the post on os.walk, and so I've found the solution to my problem.

    Sorry for cluttering the board (mod delete this!)

    ITA


    "Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

  • infidelinfidel Posts: 2,900Member
    : I've got a program which will eventually need to play a load of images from a directory in sequence for an animation.
    :
    : What I want to do is to read the filenames of all these images from the directory into a list so that I can then iterate over the list with something like [code]for x in thelistoffilenames. [/code]
    :
    : What's the best way to do this?
    :
    : Please help, many thanks.
    :
    : ITA
    :
    : "Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

    >>> os.listdir(r'K:TEMP')
    ['vnc_properties.gif', 'edits_effective_date.gif']

    I actually didn't know this off the top of my head, but figured there must be something available. Are you familiar with the dir() and help() built-in functions? They're quite handy in interactive mode for examining objects and reading the built-in documentation.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • infidelinfidel Posts: 2,900Member
    : Sorry, I've been foolish once again - I should have read the rest (or at least searched) the rest of the forum to find the answer before I posted!
    :
    : Looking further down, I found the post on os.walk, and so I've found the solution to my problem.

    os.walk is great if you have a tree of directories to walk through. If you just need a list of files in a single known directory, os.listdir would be my choice.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • ITAITA Posts: 463Member
    Thank you - maybe I wasn't so stupid after all!

    Regards,

    ITA
    "Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

Sign In or Register to comment.