I have a zip file which contains a text file(with millions of lines). I need to read line by line, apply some transformations to each line and write to a new file and zip it.
with zipfile.ZipFile("orginal.zip") as zf, zipfile.ZipFile("new.zip", "w") as new_zip:
with io.TextIOWrapper(zf.open("orginal_file.txt"), encoding="UTF-8") as fp, open("new.txt", "w") as new_txt:
for line in fp:
new_txt.write(f"{line} - NEW") # Some transformation
new_zip.writestr("new.txt", new_txt)
But I am getting following error in new_zip.writestr("new.txt", new_txt)
TypeError: object of type '_io.TextIOWrapper' has no len()
- If I do transformation using the above method, will there be any out of memory issue(since the file can have millions of lines)?
- How to identify the first line(since the first line is a header record)?
- When I write using
new_txt.write(f"{line} - NEW"),- NEWcomes first in the line(For ex. if line is003000000011000000, the output will be- NEW003000000011000000). - How can we ensure the file integrity(for ex. to ensure whether all lines are written in the new file.)
- What causes the
TypeError: object of type '_io.TextIOWrapper' has no len()error?
Thank You.