List of files to be opened

James Stroud jstroud at
Wed Jan 25 19:33:41 EST 2006

yawgmoth7 wrote:
> Hello, I am currently writing a script that requires a few different
> files to be opened, and examined. What I need to be able to do is use
> something like:
> filelist = os.system("ls")
> <Some way to open the file list and read each file one by one here
> I cannot think of a way to do this, I could put them in a list of
> something of the sort. But that still does not solve the problem of
> opening them one by one.
> Thanks for all the advice and help.
> --
> Gurus'Network - Are you a guru?

See os, os.path. Then check out the fileinput module.

A common pattern is:

  for afilename in os.listdir(apath):
    if os.path.isfile(os.path.join(apath, afilename)):
      for aline in open(os.path.join(apath, afilename)):
        # do something with aline

This allows you to do something with each line of each file in a directory.

Here are some urls that you will find helpful for these kind of things:


More information about the Python-list mailing list