Skip to main content

tempdrive

tempdrive is a Python library defining functions and context managers to create and remove Windows disk drive substitutions, i.e. virtual disk drives that act as an alias to a directory. (Just like the classic subst.exe does.)

This functionality can be useful to get around the 260-character path length limit present in many Windows applications.

Class tempdrive.DriveLetter

This class represents a Windows disk drive letter. It can be both initialized and queryied in a variety of formats. (See DriveLetter.is_drive_string().)

Properties:

  • letter: str: Just the uppercase drive letter, eg. C
  • device: str: The uppercase drive letter, plus a colon, eg. C:
  • path: pathlib.Path: Path of the root folder of the drive, eg. C:\

Static methods:

  • is_drive_string(s: str): Returns whether s is a valid drive string, i.e. one of the following (X can be any lower or upper case English letter): "X", "X:", "X:\", "X:/".

Context manager tempdrive.temporary_drive()

Context manager to create a new drive substitution (using tempdrive.subst()) for the specified path with an automatically chosen drive letter. Remove drive substitution on exit.

When used in a with statement, the target of the as clause will be set to a pathlib.Path pointing to the newly created drive's root folder.

This function can be useful if a script needs to work within a deeply nested folder, and it starts hitting the 260-character path length limit.

Arguments:

  • path: pathlib.Path: Path the newly created drive substitution should point to.
  • log: Callable[[str], None]: Logging function to use for informational messages upon adding and removing the drive substitution. If unspecified or set to None, no messages are emitted.

Raises tempdrive.TempDriveError if there is no available drive letter to use. Raises OSError if it runs into an error while calling WinAPI functions.

Function tempdrive.subst()

Creates new drive substitution, similarly to subst.exe.

For example, tempdrive.subst(tempdrive.DriveLetter("w:"), Path(r'c:\Windows')) creates a virtual W: drive that acts as an alias to the Windows folder.

Substituted drives can be useful if an application starts hitting the 260-character path length limit.

Arguments:

  • drive: tempdrive.DriveLetter: Drive letter to use for newly created drive substitution.
  • path: pathlib.Path: Path the newly created drive substitution should point to.

Raises tempdrive.TempDriveError if drive is already in use. Raises OSError if it runs into an error while calling WinAPI functions.

Function tempdrive.unsubst()

Removes a drive substitution created by tempdrive.subst().

Arguments:

  • drive: tempdrive.DriveLetter: Drive letter of substitution to remove.

Raises tempdrive.TempDriveError if the drive does not exist, or is not a substitution. Raises OSError if it runs into an error while calling WinAPI functions.

Function get_used_drive_letters()

Returns the list of tempdrive.DriveLetter objects that represent all disk drives currently present in the system. This includes:

  • Physical drives
  • Virtual/substituted disk drives
  • Connected network drives
  • Disconnected but registered network drives
  • ...

Raises OSError if it runs into an error while calling WinAPI functions.

Function get_free_drive_letters()

Returns the list of tempdrive.DriveLetter objects that represent the unassigned drive letters of the system, available for drive substitution using tempdrive.subst().

Note that this list never includes A: and B: because they were historically used for floppy disk drives, and Windows handles them specially, so it's ill-advised to use them for other purposes.

Raises OSError if it runs into an error while calling WinAPI functions.

Licensing

This library is licensed under the MIT license.

Metadata

Release files for tempdrive 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tempdrive 1.1
File Size Uploaded
tempdrive-1.1.tar.gz 23.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tempdrive 1.1
File Interpreter ABI Platform
tempdrive-1.1-py3-none-any.whl Python 3 none any Details

Total release size: 33.6 kB

Release files / tempdrive-1.1.tar.gz

Download URL tempdrive-1.1.tar.gz
Size 23.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f8f1f30b4523270f3f17c57b001bb96139587a2983d6c1dfa69fe553727873eb
BLAKE2b-256 checksum
How to use checksums
d069a34e8e56035b50007749304c744d1e999d13342dbbd579a3bc38de09d424
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / tempdrive-1.1-py3-none-any.whl

Download URL tempdrive-1.1-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
521447946bc314bf4b6554ca10915b03829a38afd141c2ff681a8db83e6fb582
BLAKE2b-256 checksum
How to use checksums
4bb2af12765998857b9b7d6dfbcf922be3850bf93cf6369b52bde8e797aec11e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

1.1 This release

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page