Skip to content

Path handling

File system path handling

Create

Create or generate path(s)

Action Code Details
Current working directory
os.getcwd()
Can be set using os.chdir()
From directory and given file name
os.path.join(dir_path, filename)
Temporary file path
tempfile.mktemp()
Temporary file path in directory
tempfile.mktemp(dir=dir_path)
Temporary file path with extension (suffix)
tempfile.mktemp('.txt')
Temporary directory path
tempfile.mkdtemp()
Temporary directory path in directory
tempfile.mkdtemp(dir=dir_path)
Directory path obtained interactively from user via dialog
import tkinter
from tkinter import filedialog
tkinter.Tk().withdraw()
path = tkinter.filedialog.askdirectory()
withdraw() is needed to prevent an annoying empty window from opening
File path obtained interactively from user via dialog
import tkinter
from tkinter import filedialog
tkinter.Tk().withdraw()
path = tkinter.filedialog.askopenfile()
withdraw() is needed to prevent an annoying empty window from opening

Test

Action Code Details
File/dir exists
os.path.exists(path)
File path
os.path.isfile(path)
Directory path
os.path.isdir(path)
Absolute path
os.path.isabs(path)
Paths refer to same file
os.path.samefile(path1, path2)
Is parent of path
?
Is child of path
?

Derive

Manipulate a path

Action Code Details
Filename
os.path.basename(r'C:\Files\derp.csv')
Returns derp.csv
Filename without extension
os.path.splitext(os.path.basename(r'C:\Files\derp.csv'))[0]
Returns derp
File extension
os.path.splitext(r'C:\Files\derp.csv')[1]
Returns .csv
Normalize path
os.path.normpath(path)
Absolute path
os.path.abspath(path)
Paths are relative to the working directory
Canonical path (resolving symlinks)
os.path.realpath(path)
Parent path
os.path.dirname(r'C:\Files\derp.csv')
Returns C:\Files
Parent directory name
os.path.basename(os.path.dirname((r'C:\Files\derp.csv'))
Returns Files
Get common parent path between two or more paths
os.path.commonpath(paths)
Relative path to directory
os.path.relpath(path, start=dir_path)
Join path with subdirectories and/or file
os.path.join(path, ..., dirN, file)
File path without extension
os.path.splitext(path)[0]

Extract

All snippets return a list of string paths

Action Code Details
File names
os.listdir(dir_path)
File names that match Unix filter
fnmatch.filter(os.listdir(dir_path), '*.csv')
File names that match regex pattern
[f for f in os.listdir(dir_path) if re.match('\.csv$', f)]
File paths
[os.path.join(dir_path, f) for f in os.listdir(dir_path)]
File paths that match Unix filter
glob.glob(os.path.join(path, '*.csv'))
File paths that match Unix filter, including nested entries (recursive)
glob.glob(os.path.join(path, '**\*.csv'), recursive = True)
The **\ is required for recursive search to do anything.
File paths that match regex pattern
[os.path.join(dir_path, f)
    for f in os.listdir(dir_path) if re.match(r'.+\.csv$', f)]
Split path into parent path and basename
parent_path, base_name = os.path.split(path)
Split path into file path and extension
file_path, ext = os.path.splitext(path)