On Jan 4, 8:20 pm, Google Poster <[email protected]> wrote:
> Can any of you nice folks post a snippet of how to perform a listing
> of the current directory and save it in a string?
>
> Something like this:
>
> $ setenv FILES = `ls`
>
> Bonus: Let's say that I want to convert the names of the files to
> lowercase? As 'tolower()'
I'd just like to mention one more python nicety: list comprehension.
If you wanted the filenames as a list of strings, with each made
lowercase then the following would serve well:
import os
filenames = os.listdir('.')
filenames_lower = [fn.lower() for fn in filenames]
You could also combine this into one line:
import os
filenames_lower = [fn.lower() for fn in os.listdir('.')]
Regards, Alex
--
http://mail.python.org/mailman/listinfo/python-list