0

谁能发现为什么这会失败?

import pathlib
from shlex import quote

path = 'testdir(abc)/subdir(123)'
filename = 'test'
content = \
"""hello
world
"""
pathlib.Path(path).mkdir(mode=0o770, parents=True, exist_ok=True)
md5_filename = quote(str(pathlib.Path(path) / (filename + '.txt')))
with open(md5_filename, 'w') as f:
    f.write(content)

我得到这个回溯

(tools) $ python test_filemake.py 
Traceback (most recent call last):
  File "test_filemake.py", line 13, in <module>
    with open(md5_filename, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: "'testdir(abc)/subdir(123)/test.txt'"

我想我对posix路径的理解不够好,无法理解发生了什么。如果我从路径中的目录名称中取出括号,它就可以正常工作。shlex.quote() 正在添加额外的双引号层,这似乎破坏了一些东西。

4

0 回答 0