I am trying to concatenate multiple (10-100) large files (100MB-1GB) to one using Python. I know that cat is efficient and fast, but I want to do it in Python due to repeatability and put all code as python and don't use shell scripts.
I tried:
path_with_txt_files = os.getcwd()
print("Current working directory is:",os.getcwd())
tempfiles=[f for f in os.listdir(path_with_txt_files) if f.endswith('.txt')]
print(tempfiles)
f = open("Concatenated.txt", "w")
for tempfile in tempfiles:
f.write(tempfile.read())
I expected it to be concatenated, but I obtained
Exception has occurred: AttributeError 'str' object has no attribute 'read'
I know that tempfiles is list of strings, but how to convert it to list of file handles?