I'm using python 3.6. I am trying to read a lot of (.txt) files in multiple directories. Some files have a comma in the file name, e.g. 'Proposal for Anne, Barry and Carol.txt'
.
The following code:
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
Gives me an error on reading that file:
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'
I do not want to edit the names of the files.
How can I properly read the files with comma's in the filenames?
Edit:
I'm sure the path exists.
Other files from the same directory are parsed without issues.
Trying to open the file directly from the commandline also gives: The system cannot find the path specified.
Also, I seem to be unable to rename the file, if I try to change the name through Windows File Explorer to remove the comma (or change something else), it is reset to the original filename.
Could it have something to do with file permissions?
- Or maybe is the filename too long? The full path from
Z:[..]
to[..].txt
is 270 characters long.