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 ]

Manipulating files and directories

See also std.file.

Examples

(1) common file and directory operations
import os
import shutil


def main():
    # suppose the file test.txt exists; its size: 5 bytes

    print(os.path.exists("test.txt"))  # True

    print(os.path.isfile("test.txt"))  # True

    print(os.path.isdir("test.txt"))  # False

    shutil.copy("test.txt", "copy.txt")  # make copy

    shutil.move("copy.txt", "new_name.txt")  # rename

    os.remove("new_name.txt")  # delete it

    os.mkdir("temp_dir")  # create dir.

    os.rmdir("temp_dir")  # delete empty dir.

    bak = os.getcwd()
    os.chdir("/tmp")
    print(os.getcwd())  # /tmp
    os.chdir(bak)

    print(os.path.getsize("test.txt"))  # 5 (bytes)
import std.stdio;
import std.file;

void main()
{
    // suppose the file test.txt exists; its size: 5 bytes

    writeln(exists("test.txt")); // true

    writeln(isFile("test.txt")); // true

    writeln(isDir("test.txt")); // false

    copy("test.txt", "copy.txt"); // make copy

    // fully qualified call
    std.file.rename("copy.txt", "new_name.txt"); // rename

    // fully qualified call
    std.file.remove("new_name.txt"); // delete it

    mkdir("temp_dir"); // create dir.

    rmdir("temp_dir"); // delete empty dir.

    auto bak = getcwd();
    chdir("/tmp");
    writeln(getcwd()); // /tmp
    chdir(bak);

    writeln(getSize("test.txt")); // 5 (bytes)
}

For rename() and remove() I got an error message. The compiler complained that these function names are also present in core.stdc.stdio and thus they conflict. It seems core.stdc.stdio is imported implicitly. However, with a fully qualified function call (i.e. std.file.remove()), the conflict can be resolved.

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, 21:32