提问人:jonafinmicclen 提问时间:9/25/2023 最后编辑:jonafinmicclen 更新时间:10/23/2023 访问量:68
在 Python 中打开 open() 函数无法处理的文件
Open file in Python that the open() function cannot handle
问:
在 python 中,尝试打开使用 glob.glob 获取的文件路径,我怀疑该路径包含的字符编码与 open() 可以处理的编码不同,不确定。我可以跳过该文件,但我需要一种方法来检测导致问题的字符。或者更确切地说,无论如何打开文件的方法将是完美的。
尝试打开许多文件,其中一个带有该名称的文件将无法打开。得到一个错误):.\\input\\537700 Raven-Symone, Anneliese van der Pol, Orlando Brown - That's So Raven! (Theme Song)\\Raven-Symone, Anneliese van der Pol, Orlando Brown - That's So Raven! (Theme Song) (fieryrage) [mk's hard].osu
files = glob.glob(folder+'*.osu', recursive = True)
for file in files:
inputFile = open(file, 'r', encoding='utf-8')
Exception has occurred: FileNotFoundError
[Errno 2] No such file or directory: ".\\input\\537700 Raven-Symone, Anneliese van der Pol, Orlando Brown - That's So Raven! (Theme Song)\\Raven-Symone, Anneliese van der Pol, Orlando Brown - That's So Raven! (Theme Song) (fieryrage) [mk's hard].osu"
File "C:\Users\Jonathan McClen\Documents\SliderToCircleOsuMapConversion\new.py", line 19, in <module>
inputFile = open(file, 'r', encoding='utf-8')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: ".\\input\\537700 Raven-Symone, Anneliese van der Pol, Orlando Brown - That's So Raven! (Theme Song)\\Raven-Symone, Anneliese van der Pol, Orlando Brown - That's So Raven! (Theme Song) (fieryrage) [mk's hard].osu"
答:
0赞
jonafinmicclen
9/25/2023
#1
问题是由文件路径上的 Windows 上限引起的 最大路径 limiation
评论
37700 Raven-Symone, Anneliese van der Pol, Orlando Brown - That's So Raven! (Theme Song)
Raven-Symone, Anneliese van der Pol, Orlando Brown - That's So Raven! (Theme Song) (fieryrage) [mk's hard]
print(os.listdir('input'))