Trying to decide between PHP and Python

Benjamin Kaplan benjamin.kaplan at case.edu
Tue Jan 4 15:45:12 EST 2011


On Tue, Jan 4, 2011 at 3:20 PM, Google Poster <goposter at jonjay.com> wrote:
>
> About once a year, I have to learn yet another programming language.
> Given all the recommendations (an outstanding accolade from Bruce
> Eckel, author of "Thinking in Java") I have set my aim to Python.
> Sounds kinda cool.
>
> The indentation-as-block is unique, but that is how I always indent,
> anyway.
>
> 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`
>


import os
FILES = os.listdir('.')

will give you a list of all the files in the current directory. If you
wanted that as a single string, just join all of the file names with
whatever you want as a separator.

files_str = '\n'.join(FILES)

It's a bit different than what your original command does- it doesn't
use the external "ls" command, it just gives you the list of files
already parsed.

> Bonus: Let's say that I want to convert the names of the files to
> lowercase? As 'tolower()'
>

Strings in Python have a lower() method.
files_str = files_str.lower()



> TIA,
>
> -Ramon
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list