[Python] Python generating a text file with a way larger size than expected

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to write the data recorded through a microphone into a text file for encryption.

    Here is my code,

    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 44100
    RECORD_SECONDS = 5

    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    frames_per_buffer=CHUNK)

    frames = []

    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

    with open("Original_Data.txt", 'wb') as outFile:
    for frame in frames:
    outFile.write(str(frame))


    Clearly, the audio data is stored in a list:

    frames[]


    There will be total of (44100/1024)*5 = 215 CHUNKS, so 215*1024 = 220160 bits.

    The file size should be,

    220161 bits = 220161/8 = 27520 bytes


    But the file that is created after the above code execute has a size of approximately 880640 bytes.

    Why is this happening?

    What I can anticipate is that, it is happening at the line when I am writing the data into a text file,

    with open("Original_Data.txt", 'wb') as outFile:
    for frame in frames:
    outFile.write(str(frame))


    But I am still not sure. Why is it happening?

    Continue reading...

Compartilhe esta Página