Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2024/25/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Get the content of a directory

See also std.file.

(1) os.listdir()
import os

def main():
    entries = os.listdir(".")

    # get all files in the current dir.
    # (non-recursively)
    for e in entries:
        if os.path.isfile(e):
            print(e)
import std.stdio;
import std.file;
import std.path; // baseName()
import std.algorithm; // map()

void main()
{
    // .: current directory
    // *: collect everything
    // SpanMode.shallow: non-recursively
    auto entries = dirEntries(".", "*", SpanMode.shallow);

    // get all files in the current dir. (non-recursively)
    foreach (e; entries)
    {
        if (isFile(e))
        {
            writeln(e);              // ./main.py
            // writeln(e.baseName);  // main.py
        }
    }
}
main.py
main.d
test.txt
./main.py
./main.d
./test.txt

In the D version, we also get the relative path in front of the filename, e.g. ./main.py . If you don't need that prefix, use the second version with baseName().

Glob

Let's say you want to collect all the .txt files in the current directory.

(2) glob.glob("*.txt")
import glob

def main():
    entries = glob.glob("*.txt")

    for e in entries:
        print(e)
import std.stdio;
import std.file; // dirEntries()
import std.path; // baseName()

void main()
{
    // .:     current directory
    // *.txt: wildcard, what to collect
    // SpanMode.shallow: non-recursively
    auto entries = dirEntries(".", "*.txt", SpanMode.shallow);

    foreach (e; entries)
    {
        writeln(e);
        // writeln(e.baseName);
    }
}
in.txt
out.txt
test.txt
./in.txt
./out.txt
./test.txt

In the D version, we also get the relative path in front of the filename, e.g. ./main.py . If you don't need that prefix, use the second version with baseName().

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 July 01, 22:24