import os # Variant 2 def solve(dir_files, dir_result, filename_result): filenames = os.listdir(dir_files) result = '' for filename in filenames: filepath = os.path.join(dir_files, filename) file = open(filepath, "r") result += f"{file.readline()}" file.close() if not os.path.exists(dir_result): os.makedirs(dir_result) if os.listdir(dir_result): return result_file = open(os.path.join(dir_result, filename_result), "w") result_file.write(result) result_file.close() if __name__ == "__main__": solve('/var/data', '/var/result', 'data.txt')