This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: There is a problem with escape characters in the path passed in by pathlib.Path.mkdir()
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, paul.moore, steve.dower, tim.golden, vincent.fung, zach.ware
Priority: normal Keywords:

Created on 2022-03-03 22:17 by vincent.fung, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (7)
msg414483 - (view) Author: Vincent FUNG (vincent.fung) Date: 2022-03-03 22:17
This problem occurs when the directory starts with 't', but works with os.makedirs(e) or macOS.

>>> e = Path(r'F:\ceven\test2')
>>> e.mkdir()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python310\lib\pathlib.py", line 1173, in mkdir
    self._accessor.mkdir(self, mode)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'F:\\ceven\\test2'
>>> os.makedirs(e)
msg414487 - (view) Author: Vincent FUNG (vincent.fung) Date: 2022-03-03 22:58
This problem occurs when the directory starts with 't', but works with os.makedirs(e) or macOS.

>>> e = Path(r'F:\ceven\test2')
>>> e.mkdir()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python310\lib\pathlib.py", line 1173, in mkdir
    self._accessor.mkdir(self, mode)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'F:\\ceven\\test2'
>>> os.makedirs(e)



Another question about \t:

If a directory is passed as a parameter it will not be possible to use the parameter as a raw string. os.makedirs gives the same error whether it's r'%s' or repr().
msg414503 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-03-04 02:00
> FileNotFoundError: [WinError 3] The system cannot find 
> the path specified: 'F:\\ceven\\test2'

The Windows error code, ERROR_PATH_NOT_FOUND (3), indicates that the parent path, r"F:\ceven", does not exist. Try e.mkdir(parents=True) [1].

[1] https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir
msg414504 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-03-04 02:30
> e = Path(r'F:\ceven\test2')

Using forward slashes in the string literal is preferred, e.g. Path('F:/ceven/test2'). This avoids the problem of backslash escapes in string literals. The Path constructor parses the path and stores it internally as component parts. When the path object is needed as a string, os.fspath() returns the path using the platform's preferred path separator. A WindowsPath or PureWindowsPath uses backslash as the path separator. For example:

    >>> os.fspath(pathlib.PureWindowsPath('F:/ceven/test2'))
    'F:\\ceven\\test2'
msg414514 - (view) Author: Vincent FUNG (vincent.fung) Date: 2022-03-04 10:56
Oh. Got it. 

I thought pathlib would solve this problem completely now, without having to replace the slashes. Thank you for your patient answer.
msg414517 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-03-04 11:43
> I thought pathlib would solve this problem completely now, 
> without having to replace the slashes.

pathlib has nothing to do with how the Python language compiles string literals. A string *literal* is Python source code that gets compiled and instantiated as a string object. 

There is no technical problem with using r'F:\ceven\test2'. Using a raw string literal disables backslash escapes. But many developers find it inconvenient to have to use raw strings or to have to escape backslashes by doubling them. Plus raw string literals can't represent a path that ends with a backslash, such as C:\. 

The Windows API supports forward slash as a path separator in many cases, but not always, and also not always for paths passed on the command line. So it's preferable to always normalize paths. pathlib takes care of this for you. You can initialize conveniently with forward slashes, but get a Windows path string that uses backslashes.
msg414524 - (view) Author: Vincent FUNG (vincent.fung) Date: 2022-03-04 13:21
Thank you very much for your patient answer, I am still a developer who has just started, and it seems that I need to learn more about it.

I used repr().replace to replace with forward slashes, and also restricted paths ending in a slash to Improve reliability.

Thanks anagin. ;)
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 91072
2022-03-04 13:21:07vincent.fungsetmessages: + msg414524
2022-03-04 11:43:00eryksunsetmessages: + msg414517
2022-03-04 10:56:33vincent.fungsetstatus: open -> closed
resolution: not a bug
messages: + msg414514

stage: resolved
2022-03-04 02:30:52eryksunsetmessages: + msg414504
2022-03-04 02:00:15eryksunsetnosy: + eryksun
messages: + msg414503
2022-03-03 22:58:43vincent.fungsetmessages: + msg414487
2022-03-03 22:17:38vincent.fungcreate