Skip to main content

Escaping file paths (Windows) without crying and losing one's mind

Project description

Escaping file paths (Windows) without crying and losing one's mind

pip install escape-windows-filepath

Tested against Windows 10 / Python 3.10 / Anaconda

# How it is usually done:

import os

videopathraw = r"C:\baba ''bubu\bab xx\2020-11-23 13-05-26.mp4"

vlcpathraw = r"C:\Program Files\VideoLAN\VLC\vlc.exe"



# Quite frequently something like this surprisingly works, but in this example it doesn't:

videofilenotwellescaped = '"' + r"C:\baba ''bubu\bab xx\2020-11-23 13-05-26.mp4" + '"'

vlcpathnotwellescaped = '"' + r"C:\Program Files\VideoLAN\VLC\vlc.exe" + '"'

escapedcmdnotgood = rf"{vlcpathnotwellescaped} {videofilenotwellescaped}"

print(f"{escapedcmdnotgood=}")

os.system(escapedcmdnotgood)



# Output:

# escapedcmdnotgood='"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe" "C:\\baba \'\'bubu\\bab xx\\2020-11-23 13-05-26.mp4"'

# 'C:\Program' is not recognized as an internal or external command,

# operable program or batch file.





from escape_windows_filepath import escape_windows_path



# The safest method is escaping evey single part (folder,file) of the path

videofileescaped = escape_windows_path(videopathraw)

vlcpath = escape_windows_path(vlcpathraw)

escapedcmd = rf"{vlcpath} {videofileescaped}"

print(f"{escapedcmd=}")

os.system(escapedcmd)

# Output

# escapedcmd='C:\\"Program Files"\\"VideoLAN"\\"VLC"\\"vlc.exe" C:\\"baba \'\'bubu"\\"bab xx"\\"2020-11-23 13-05-26.mp4"'

# And the video player opens

Project details


Release history Release notifications | RSS feed

This version

0.10

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

escape_windows_filepath-0.10.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

escape_windows_filepath-0.10-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file escape_windows_filepath-0.10.tar.gz.

File metadata

File hashes

Hashes for escape_windows_filepath-0.10.tar.gz
Algorithm Hash digest
SHA256 831e331521a3881ed11bc603a5c8709b4c27507ad3482256ae8a6cb7b85d84bf
MD5 0b733aab274f2d372f15c30ece461ddf
BLAKE2b-256 82c4d58c5155e0bca3db9d098fabddf0e79a41bf802d5c9dcf9d96714991ce54

See more details on using hashes here.

File details

Details for the file escape_windows_filepath-0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for escape_windows_filepath-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 d1eeb761460c9498008157abc8851e0a64e39270e256ac59215f89773bd015f3
MD5 4b4f201665fc404c8706bcd82dd3a926
BLAKE2b-256 ae1267d81ee2928087d6c4eecdc487001d4df73bc6d12be08697449afa2320d9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page