Trying to decide between PHP and Python
Alex Willmer
alex at moreati.org.uk
Tue Jan 4 16:09:13 EST 2011
On Jan 4, 8:20 pm, Google Poster <gopos... at jonjay.com> 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
More information about the Python-list
mailing list