简体   繁体   English

在Python中使用pathlib(\\ u2069)弄错文件夹

[英]In Python Getting the folder wrong with pathlib ( \u2069 )

I can not make the following code work in my mac and I don't know why. 我无法在Mac中运行以下代码,也不知道为什么。

import os
from pathlib import Path   
list_file_name     = 'listoffiles.txt'
list_of_files_path = Path('Users⁩/jose⁩/CODE⁩/OPS')
list_file_with_path= os.path.join (list_of_files_path,list_file_name )
print(list_of_files_path)

result: Users⁩/jose⁩/CODE⁩/OPS/listoffiles.txt which seems to be ok 结果:Users⁩/jose⁩/CODE⁩/ OPS / listoffiles.txt似乎还可以

but when I go on in order to read the text file into a list: 但是当我继续以将文本文件读入列表时:

lineList = [line.rstrip('\n') for line in open(list_file_with_path)]

I get: FileNotFoundError: [Errno 2] No such file or directory: 'Users\⁩/jose\⁩/CODE\⁩/OPS/listoffiles.txt' 我得到:FileNotFoundError:[Errno 2]没有这样的文件或目录:'Users \\ u2069 / jose \\ u2069 / CODE \\ u2069 / OPS / listoffiles.txt'

I don't get how do I have to call the path in order to get it right. 我不知道如何调用该路径才能正确处理它。

Some help? 一些帮助? what I am doing wrong? 我做错了什么?

NOTES: researching the problem in internet I found a couple of pages telling that the "old" way to work with paths in with os library, whereas the "new and good one" is pathlib from python 3.4. 注意:研究Internet中的问题时,我发现有几页告诉我们,使用os库的“旧”方式可以使用路径,而“新的好方式”是python 3.4中的pathlib。 Is that so. 是这样吗。 Should I forget os? 我应该忘记操作系统吗?

The string in your sample code above has the Unicode characters directly entered in the string constant. 上面的示例代码中的字符串在字符串常量中直接输入了Unicode字符。 I cut-n-pasted your code and see the result: 我剪切了您的代码,然后看到结果:

from pathlib import Path   
list_of_files_path = Path('Users⁩/jose⁩/CODE⁩/OPS')
print(repr(list_of_files_path))

Output: 输出:

WindowsPath('Users\u2069/jose\u2069/CODE\u2069/OPS')

My editor even displays them: 我的编辑器甚至显示它们:

编辑器带有控制字符的象形文字

U+2069 is an invisible bidirectional text control character POP DIRECTIONAL ISOLATE . U + 2069是一个不可见的双向文本控制字符POP DIRECTIONAL ISOLATE Maybe your editor was in a bidrectional text mode when it was typed? 键入时,您的编辑器可能处于双向文本模式? To fix, retype the string and make sure via repr() that there are no more control characters. 要解决此问题,请重新键入字符串,并通过repr()确保没有更多的控制字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM