Access all classes in a directory, put into an array, and print them out

Alex Martelli aleaxit at yahoo.com
Tue Jul 3 12:08:01 EDT 2001


"Kemp Randy-W18971" <Randy.L.Kemp at motorola.com> wrote in message
news:mailman.994168205.8845.python-list at python.org...
> Can anyone share a short code example of how to access all the programs in
a
> Unix directory, put them into an array, and print them out in Python?
Here
> is the code I have to do it in Perl (please forgive the harsh language).
>
> #!/usr/bin/perl -w
> #use Net::FTP;
> opendir (TEMP, '/usr2/ecadtesting/javaprograms')
>   or die "Can't open current directory.";
> @files=join(',',grep(!/^\.\.?$/, readdir TEMP));
> closedir TEMP;
> print "\n";
> print @files;
> print "\n";

#!/usr/bin/python
import os
print os.listdir('/usr2/ecadtesting/javaprograms')

Makes you wonder where Perl got its reputation for
conciseness, doesn't it?-)  [Note the . and .. entries
are automatically removed by the listdir function].


Alex






More information about the Python-list mailing list