Python Short Demo: Set Operating System-Agnostic Directory Paths for Files using the pathlib Module

Different computer operating systems (OS) use different syntaxes to set filesytem paths: Linux & MacOS use forward slashes, while Windows uses back slashes. This can cause errors when users enter directory paths in Python scripts. An easy way to avoid this problem is to use the pathlib module, which automatically sets filesystem paths using the correct syntax for the user's OS.

Please acknowledge the NOAA/NESDIS/STAR Aerosols and Atmospheric Composition Science Team if using any of this code in your work/research!

# Module to set filesystem paths appropriate for user's operating system
from pathlib import Path

# Set directory as the current working directory (cwd)
cwd_path = Path.cwd()

# Set directory using entered directory name
directory_name = 'D://Data/Tests/'  # Enter directory name as a string
directory_path = Path(directory_name) 

Demonstration of how to use the module: The user enters the "directory_name" as a string, or sets the current working directory (cwd). In this example, the file paths are on a computer running Windows.

  • To set the path for a single file in a directory ("file_path"), the user enters the "file_name" as a string and then combines the directory and file name using "/".
  • To set the path to collect multiple files in a directory matching a specified pattern, the user enters a wildcard search string pattern in the "Path(directory_name).glob(pattern)" function, e.g., "Path(directory_name).glob('*.nc')" to collect all files ending with an ".nc" extension.
# Set path for a single file in a directory
file_name = ''  # Enter file name
file_path = directory_path / file_name

# Print file path for a single file


# Set path to collect multiple files in a directory
# Use wildcard string to find files, e.g. all .nc files
file_list = directory_path.glob('*.nc')

# Print file paths for multiple files collected in a directory
for file_path in file_list: