The nturl2path module

(Implementation) The nturl2path module, shown in Example 13-15, contains code to map between URLs and Windows filenames.

Example 13-15. Using the nturl2path Module

File: nturl2path-example-1.py

import nturl2path

file = r"c:\my\little\pony"

print nturl2path.pathname2url(file)
print nturl2path.url2pathname(nturl2path.pathname2url(file))

///C|/my/little/pony
C:\my\little\pony

This module should not be used directly; for portability, access these functions via the urllib module instead, as shown in Example 13-16.

Example 13-16. Using the nturl2path Module via the urllib Module

File: nturl2path-example-2.py

import urllib

file = r"c:\my\little\pony"

print urllib.pathname2url(file)
print urllib.url2pathname(urllib.pathname2url(file))

///C|/my/little/pony
C:\my\little\pony

Get Python Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.