1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import os
path=os.path.join(os.path.expanduser("~"), "Desktop", "Python", "Concatenation_PY")
path_i=os.path.join(path, "Drop files")
inputs=tuple(f for f in os.listdir(path_i) if f.endswith('.txt')) # Si la liste n'évolue pas, alors autant passer par un tuple plus économique
with open(os.path.join(path, 'Output.txt'), 'w') as outfile:
for (i, fname) in enumerate(inputs, 1):
with open(os.path.join(path_i, fname), "r", encoding='utf-8', errors='ignore') as infile:
if i > 1: print("", file=outfile)
outfile.write(infile.read())
# with
# for
# with
print("The program has concatenated {} file{}".format(i, "s" if i > 1 else "")) |
Partager