pythonbatch-filefile-renamefile-copying

Copy a bunch of .wav files, change the content to another .wav sound, but keep the names


I got a folder filled with sounds, and I want to replace all of the sounds to a single sound I have, but I want all the files to keep their original name, would this be possible?

I don't have any idea on how I could do this honestly.


Solution

  • Something like this? I am a little confused with the sounds, but tried to understand it.

    import os, wave, shutil
    
    my_sound = "sound.wav"
    my_dir = "/tests/sounds/"
    
    for filename in os.listdir(my_dir):
        if filename.endswith(".wav"):
            file_path = os.path.join(my_dir, filename)
            with wave.open(file_path, 'wb') as test_wave:
                with wave.open(my_sound, 'rb') as original_wave:
                    test_wave.setparams(original_wave.getparams())
                    test_wave.writeframes(original_wave.readframes(original_wave.getnframes()))