Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compressing a file
#1
i have a text file. i want to read the file in a compressed form (in binary, of course). lzma.open() seems to do the opposite of what i need. it uncompresses but i need to do compression. how can i do compression and reading? i want to avoid compressing in memory.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
It is difficult to determine exactly what you are asking but the following code will read any file as bytes. Is this what you are looking for?

with open ('document.txt', 'rb') as input_file :
	the_file_data = input_file.read ()
Reply
#3
using lzma.open() to read a file can only uncompress. to do compression, it must be used to write a file. i want to read a file and compress it so i end up with the compressed bytes.

alas, after some other design changes, that file no long is on disk. i will have it in memory., so, i can use lzma.LZMACompressor() to do it all in memory and end up with the compressed bytes.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
So you want to read an uncompressed source file and compress it, but did not want the compression to be done in memory? Where did you want the compression to occur?
Reply
#5
(Jun-02-2021, 08:53 PM)jefsummers Wrote: So you want to read an uncompressed source file and compress it, but did not want the compression to be done in memory? Where did you want the compression to occur?

I interpreted the question as wanting to compress it, but read the data as a stream so the entire file is not in memory at one time. The docs suggested to me that compressing a single object is simple, but if you wanted a stream compressor, that's not explicitly shown.

I would assume there's some way to chain the blockIO devices so you could read() the compression results (rather than direct it to a file on disk), but wasn't sure how to accomplish that.
Reply
#6
(Jun-02-2021, 08:53 PM)jefsummers Wrote: So you want to read an uncompressed source file and compress it, but did not want the compression to be done in memory? Where did you want the compression to occur?

i did not want a solution that involved reading the whole file into memory and then compressing because it would take too much memory. i wanted to do incremental compression to use less memory. i've found a way to do it as the file is generated so i don't have to keep the uncompressed data.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] compressing files with python. SpongeB0B 1 649 May-26-2023, 03:33 PM
Last Post: SpongeB0B

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020