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

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ✦C++, ✦Clojure, ✦Scala


[ edit | logout ]
[ sandbox | passwd ]

Python's os and os.path modules

Here I couldn't put Python and Nim next to each other, the columns were too wide. You'll find Nim after the Python section.

Python

os and os.path modules + glob
=============================

https://docs.python.org/3/library/os.html

    import os

    cwd = os.getcwd()           Where are we currently, in which directory?
    os.chdir("/tmp")            change directory

    os.mkdir("hello")           create a folder
    os.rmdir("hello")           delete an (empty) folder

    os.listdir(".")             contents of the given folder
    os.listdir("data/")         contents of the data/ subdirectory


https://docs.python.org/3/library/os.path.html

    import os

    # No need for `import os.path`!
    # After `import os`, `os.path` is also available

    os.path.exists(entry)       Does the entry exist?
    os.path.isfile(entry)       Is the entry a file?
    os.path.isdir(entry)        Is the entry a directory?

    os.rename(old, new)         rename a file

    os.remove(fname)            delete a file
    os.unlink(fname)            delete a file

https://docs.python.org/3/library/glob.html

    from glob import glob

    mp3s = glob("*.mp3")        Using the patterns familiar from bash.
    files = glob("data/*.txt")

Nim

See also std/os .

os module + walkDir / walkFiles
===============================

https://nim-lang.org/docs/os.html

    import std/os

    let cwd = getCurrentDir()        Where are we currently, in which directory?
    setCurrentDir("/tmp")            change directory

    createDir("hello")               create a folder
    removeDir("hello")               delete a folder (even if it's not empty!)

    for entry in walkDir("."):       contents of the given folder
      echo entry.path                  entry.path starts with "./"

    for entry in walkDir("data/"):   contents of the data/ subdirectory
      echo entry.path                  entry.path starts with "data/"

    # No separate os.path import needed!
    # Everything is available after `import os`

    fileExists(entry) or dirExists(entry)    Does the entry exist? (no single `exists`)
    fileExists(entry)                        Is the entry a file?
    dirExists(entry)                         Is the entry a directory?

    moveFile(old, new)                       rename / move a file

    removeFile(fname)                        delete a file (no unlink — only removeFile)

    # Glob — built-in, no separate package needed
    # Uses the same *, ? patterns as bash

    import std/sequtils  # toSeq()

    let mp3s  = toSeq(walkFiles("*.mp3"))
    let files = toSeq(walkFiles("data/*.txt"))

    # For recursive glob:
    for f in walkDirRec("."):                it iterates over files only
      echo f                                   f: filename with path

File size

file size
import os

fname = "/tmp/send/sample.mp3"

print(os.path.getsize(fname))  # 161286928 ; in bytes
import std/os

let fname = "/tmp/send/sample.mp3"

echo getFileSize(fname)  # 161286928 ; in bytes
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 2026 May 19, 12:34