0

I have an assignment where I have to apply the Lempel-Ziv Algorithm (Python). I have to read an input file build a program that compresses it and generates the compressed file, and then build another program that decompresses the compressed file back to the initial one. My code is working fine for most files, but not for all. My problem is in the way that I read and write the files.

I convert the input file in a binary string in order to manipulate it (compression). Then I have to store that string in an intermediate file. Then use the string in the intermediate file to manipulate it (decompression) and generate the final file.

To a specific file I got the error "\ No newline at end of file" when I test the differences between the original file and the decompressed one.

Here https://github.com/PedroAlexLeite/LZ you have access to the code and the file that is giving me this error.

Thanks in advance.

  • In your output file, write the last character expected in a file, which should be the `\0` character. – Shmack Jan 31 '23 at 15:02
  • I'm not a 100% certain, but I think you could bypass this problem if you import BytesIO from io, and store your "binary string" in that buffer, after you've completed your conversion, then open the file for writing and then dump the BytesIO buffer. – Shmack Jan 31 '23 at 15:06
  • @Shmack Thanks for your response. I'm not sure how to apply that to my code. In "compress.py" how do I read the input file using BytesIO (input_string)? And then what do I write to the outputfile? Do I do the same in "decompress.py"? – Pedro Leite Jan 31 '23 at 16:28
  • `from io import BytesIO as bio; buffer = bio(whatever_your_string_variable_is_that_is_bytes); with open("my_file.bin", "wb") as f: f.write(buffer)` to write it out. – Shmack Jan 31 '23 at 21:37
  • `from io import BytesIO as bio; buffer = bio(whatever_your_string_variable_is_that_is_bytes); with open("my_file.bin", "rb") as f: buffer = bio(f.read())` to read the bytes from a file. I think its `.read()` - can't remember off the top of my head. `.readlines()` should return a list - which you don't want to feed into BytesIO, so it should be `.read()`. – Shmack Jan 31 '23 at 21:39

0 Answers0