os

Create path if not exists

import os
if not os.path.exists(path):
   os.mkdir(path)

filename = os.path.basename(path)
f, ext = os.path.splitext(filename)

os.environ.get(“key”, “default”)

Get files list

from glob import glob
files = glob(“*.csv”)

Split filename

# filename = "hello.txt"
# path = "/home/myname/hello.txt"
# f = "hello"
# ext = "txt"

filename = os.path.basename(path)
f, ext = os.path.splitext(filename)

Remove

import os, shutil

# remove file
if os.path.isfile("unwanted-file.txt"):
    os.remove("unwanted-file.txt")

# remove empty folder
if os.path.isdir("empty-dir"):
    os.rmdir("empty-dir")

# remove non-empty folder
if os.path.isdir("nonempty-dir"):
    shutil.rmtree("nonempty-dir")

Last updated