我正在使用python 3.6。我正在尝试在多个目录中读取很多(.txt)文件。某些文件的文件名中带有逗号,例如'Proposal for Anne, Barry and Carol.txt'
。
以下代码:
for filepath in glob.iglob(params.input_dir + r'\**\**.*', recursive=True):
# [not shown here: code that filters on .txt filetype]
with open(filepath) as f:
for line in f:
for word in re.findall(r'\w+', line):
# do stuff
在读取该文件时给我一个错误:
Traceback (most recent call last):
File "dir_scraper.py", line 50, in <module>
results_new = scraper.scrape_file(filepath)
File "C:\Projects\scraper.py", line 33, in scrape_file
return func(filepath)
File "C:\Projects\scraper.py", line 15, in txt
with open(filepath) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'Z:\\groups\\Proposal for Anne, Barry and Carol.txt'
我不想编辑文件名。
如何正确读取文件名中带有逗号的文件?
编辑:
我确定路径存在。
同一目录中的其他文件也可以正确解析。
尝试直接从命令行打开文件还会显示:系统找不到指定的路径。
此外,我似乎无法重命名该文件,如果我尝试通过Windows File Explorer更改名称以删除逗号(或进行其他更改),则会将其重置为原始文件名。
它与文件权限有关吗?
Z:[..]
到[..].txt
的完整路径为270个字符。答案 0 :(得分:0)
这在Python 3,Windows 10上正常工作
import glob, re
for filepath in glob.iglob('C:/Users/test-ABC/Desktop/test/' + r'\**\**.*', recursive=True):
with open(filepath) as f:
print(f)
for line in f:
print(line)
for word in re.findall(r'\w+', line):
pass
<_io.TextIOWrapper
name='C:/Users/test-ABC/Desktop/test\\loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
name\\another
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
name\\test, file, name.txt' mode='r' encoding='cp1251'>
line1
line2
line3
可能是长途问题。尝试检查如下问题: Long paths in Python on Windows
答案 1 :(得分:0)
首先,您仅处理文件,而不处理目录,其次,可以使用os.path.join在Windows上进行转换:
>>>os.path.join("d:\ss")
'd:\\ss'
尝试一下:
from pathlib import Path
import os
import re
pathName='./'# r'd:/xx' on windows
fnLst=list(filter(lambda x:not x.is_dir(),Path(pathName).glob('**/*.txt')))
print(fnLst)
for fn in fnLst:
with open(fn) as f:
print()
print(fn)
for line in f:
for word in re.findall(r'\w+', line):
print(word,end="|")
输出:
[PosixPath('2.txt'), PosixPath('1.txt')]
2.txt
This|tutorial|introduces|the|reader|informally|to|the|basic|concepts|and|features|of|the|Python|language|and|system|It|helps|to|have|a|Python|interpreter|handy|for|hands|on|experience|but|all|examples|are|self|contained|so|the|tutorial|can|be|read|off|line|as|well|
1.txt
Python|is|an|easy|to|learn|powerful|programming|language|It|has|efficient|high|level|data|structures|and|a|simple|but|effective|approach|to|object|oriented|programming|Python|s|elegant|syntax|and|dynamic|typing|together|with|its|interpreted|nature|make|it|an|ideal|language|for|scripting|and|rapid|application|development|in|many|areas|on|most|platforms|