Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>os</code> library (and <code>listdir</code> in particular) should provide you with the basic tools you need:</p> <pre><code>import os print("\n".join(os.listdir())) # returns all of the files (&amp; directories) in the current directory </code></pre> <p>Obviously you'll want to call <code>open</code> with them, but this gives you the files in an iterable form (which I think is the crux of the issue you're facing). At this point you can just do a <code>for</code> loop and open them all (or some of them).</p> <p>quick caveat: Jon Clements pointed out in the comments of Henry Keiter's answer that you should watch out for directories, which will show up in <code>os.listdir</code> along with files. </p> <p>Additionally, this is a good time to write in some filtering statements to make sure you only try to open the right kinds of files. You might be thinking you'll only ever have .txt files in a directory <strong>now</strong>, but someday your operating system (or users) will have a clever idea to put something else in there, and that could throw a wrench in your code.</p> <p>Fortunately, a quick filter can do that, and you can do it a couple of ways (I'm just going to show a regex filter):</p> <pre><code>import os,re scripts=re.compile(".*\.py$") files=[open(x,'r') for x in os.listdir() if os.path.isfile(x) and scripts.match(x)] files=map(lambda x:x.read(),files) print("\n".join(files)) </code></pre> <p>Note that I'm not checking things like whether I have permission to access the file, so if I have the ability to see the file in the directory but not permission to read it then I'll hit an exception.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload