You are welcome! I did it so you can see that is not so complicated when you are doing it step by step. Tiny steps. As @Kebap said.
Look at
os.path.join(dir, subdir, filename)
Look at
os.path
module. os.path.join() takes a bunch of arguments and join them to create a properly formated path acording to the OS. os.path.join(dir, subdir, filename)
import os.path dir_ = some_dir # dir is reserved word in Python. root is more suitable variable name :-) subdir = subdir file_name = '{}.txt'.format(element[0]) path = os.path.join(dir_, subdir, file_name) with open(path, 'w') as out_file: # etc.You can use
os.path.abspath(dir)
on dir
to get the full path of the dir. As a precaution.