Dealing with files...

emf i at mindlace.net
Sat Apr 26 10:41:24 EDT 2003


lost wrote:
> thanks for the info, however that doesn't seem to work either? here's the
> whole program that I'm trying:
> #!/usr/bin/python
> from glob import glob
> file_list = glob.glob('/var/www/html/') for each_file in file_list:
>         print each_file
 > print "done"
[...]
> Any ideas where I'm going wrong?
> Thanks again...

In this case you've imported just the function glob from module glob. So 
glob('/var/www/html/') would be sufficient; however, you've got other 
problems.

You can't put a logical block inside an assignment; in this case, you 
don't do anything with file_list aside from print each thing in it.

Here's one take, using glob:

#!/usr/bin/python
from glob import glob
for filename in glob('/var/www/html/*'):
     print filename
print "done"

Here's another using os.listdir:

#!/usr/bin/python
from os import listdir
dir = '/var/www/html/'
for filename in listdir(dir):
     print dir,filename
print "done"

The advantage of the latter is you get the invisible directories or 
files and, if you eventually want to expand this to something else, you 
have just the filenames instead of the full paths.

~mindlace







More information about the Python-list mailing list