(me again… )
So, I have another question, although I feel I’m getting better at this programming ;-).
I am mostly done with my assignment, but this error keeps popping up. I understand why it happens and what is wrong, but have been unsuccesful in solving it (or in finding proper documentation to help me).
I have a list with languages that I want to run through and a directory filled with files with a txt-extension (dutch1.txt, dutch2.txt, …).
The plan is that my program takes the first language from the list and runs through the files in my source directory. If the language is in the filename, the file is added to a new file (with name dutch.txt, french.txt, …).
The adding works fine (I tried it while manually adding the reference language), but I get the error message:
argument of type WindowsPath is not iterable
I am fairly certain that is because I try to look for the language in “file” rather then filename, but I am not sure how to tell Python to look in the filename.
This is the code:
from pathlib import Path
source_dir = Path('C:/Users/gwovi/PycharmProjects/VickyGwosdz_LanguageDetector')
files = source_dir.glob('*.txt')
languages = ["dutch", "english", "french", "german", "italian", "spanish"]
for language in languages: # itereren over de verschillende elementen in de languages-lijst
for file in files: # itereren over de txt-files in de folder
# files van dezelfde taal worden achter elkaar aan geplakt in een file
if language in file:
with open(file.with_suffix('.txt'), 'r', encoding='utf8') as f: language_input = f.read()
with open('language.txt', 'a+', encoding='utf-8') as f2:
f2.write(language_input)
Thanks again for pointing me in the right direction (I will pay it forward in the forum…)