Field Note

Converting UNIX-style Paths to Windows-style Paths with Python's pathlib

python unix windows path conversion
Posted on Tuesday, September the 3rd 2024
1 min read

This is very simple to do, just import Path and PureWindowsPath from pathlib and then do the following:

# to-windows-path.py
from pathlib import Path, PureWindowsPath
import os
import sys

if __name__ == '__main__':
	args = sys.argv[1:]
	args_length = len(args)
	if args_length != 1:
		sys.exit(f"Expected exactly one argument but got {args_length}.")

	path = Path(args[0])
	pure_windows_path = PureWindowsPath(path)

	sys.stdout.write(pure_windows_path.__str__())

Example:

$ python to-windows-path.py path/to/the/file.txt
path\to\the\file.txt
friedrichkurz.me

© 2025 Friedrich Kurz

Privacy Policy