|
Oktatás * Programozás 2 * Szkriptnyelvek * Adator. prog. Teaching • Prog. for Data Sci. Félévek Linkek * kalendárium |
Nim2 /
Python's os and os.path modulesHere I couldn't put Python and Nim next to each other, the columns were too wide. You'll find Nim after the Python section. Pythonos 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") NimSee 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
|
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |
||||