Skip to main content

How to Read a Command-Line Argument as a Path in Python with argparse type=Path

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

argparse gives you every command-line argument as a string. When the argument is a file or a folder, you want a pathlib Path, so you can ask for its name or check that it exists. Pass type=Path and argparse converts it for you. This guide checks two files from the command line on Python 3.15.2.

Syntax​

Syntax
parser.add_argument("<name>", type=Path)
PartWhat it means
"<name>"The argument's name: a positional argument, or an option such as "--data-dir"
type=PathA function argparse calls on the text the user typed. Path turns it into a path object

The argparse documentation explains: "By default, the parser reads command-line arguments in as simple strings. However, quite often the command-line string should instead be interpreted as another type", and lists type=pathlib.Path among the "Common built-in types and functions" that "can be used as type converters".

The Example​

A script takes one file from the command line and prints its name, its extension and whether it exists. It runs once on a file that exists and once on one that doesn't.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

check_file.py
import argparse
from pathlib import Path

parser = argparse.ArgumentParser()
parser.add_argument("file", type=Path, help="the file to check")
args = parser.parse_args()

print(f"name: {args.file.name}")
print(f"suffix: {args.file.suffix}")
print(f"exists: {args.file.exists()}")

The check_file.py script in a macOS-style code window: an ArgumentParser with a file argument of type=Path, then prints of args.file.name, args.file.suffix and args.file.exists().

Because args.file is a Path, the code uses .name, .suffix and .exists() on it with no conversion of its own.

Step 2: Check a File That Exists​

The script checks itself:

Terminal
uv run python check_file.py check_file.py
Output
name: check_file.py
suffix: .py
exists: True

The terminal after uv run python check_file.py check_file.py: name check_file.py, suffix .py, exists True.

Step 3: Check a File That Doesn't​

Terminal
uv run python check_file.py photos/missing.png
Output
name: missing.png
suffix: .png
exists: False

The terminal after uv run python check_file.py photos/missing.png: name missing.png, suffix .png, exists False.

type=Path doesn't check that the file exists: it only converts the text, so name and suffix work for a missing file too, and exists() tells you it isn't there. The commands work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out type=Pathargs.file is a string, so args.file.exists() stops with AttributeError: 'str' object has no attribute 'exists'Add type=Path
Writing type=Path()add_argument() stops with TypeError: PosixPath('.') is not callable: Path() is already a path, not something argparse can callPass the class itself: type=Path
Expecting argparse to reject a missing fileIt accepted photos/missing.pngCheck args.file.exists() and stop with a clear message

FAQ​

Does a default value get converted too?​

Only a default given as a string. The documentation: "If the type keyword is used with the default keyword, the type converter is only applied if the default is a string." So give a Path default, default=Path("out"), or a string default, default="data": with no options on the command line, both reached the code as paths, PosixPath('data') and PosixPath('out').

Does it work for options as well?​

Yes. With parser.add_argument("--data-dir", type=Path, default="data"), running the script with --data-dir photos gave args.data_dir as PosixPath('photos'). argparse turns the dash in the option's name into an underscore.

Conclusion​

type=Path made argparse hand the script a Path instead of a string, so .name, .suffix and .exists() worked on the argument at once. It converts the text without checking it: whether the file exists is up to your code.

Additional Resources​

  • type — type converters in the argparse documentation.
  • pathlib — what you can do with a Path.