简体   繁体   中英

How to swap files with one file extension with identically named but different files with a different extension and maintain the directory structure?

I work in audio and I need a number of files transcribed by a third party. To do so I have to swap out an entire directory of .wav files with .mp3s I have compressed while still maintaining the file directory. It's about 20,000 files.

eg

wav:

Folder1
  Folder 1a
    sound1.wav
    sound2.wav
  Folder 1b
    sound3.wav
    sound4.wav
Folder2
  Folder 2a
    Folder 2aa
       sound5.wav
       sound6.wav
    Folder 2ab
      sound7.wav
  Folder2b
    sound8.wav
  etc.

mp3:

Folder1
  sound1.mp3
  sound2.mp3
  sound3.mp3
  sound4.mp3
  sound5.mp3
  sound6.mp3
  sound7.mp3
  sound8.mp3
  etc.

I had to group them together to do the batch compression in Adobe Audition, but now I would like to be able to switch them out with the wav files that are perfectly identical save for file extension as doing this manually is not a reasonable option.

Any help would be greatly appreciated. I have a little experience with python so that language is preferable, but I'm open to any solutions.

You can use a combination of glob and shutil to do this. Try running this script from inside Folder1 .

from glob import glob
from shutil import move
import os

wav_files = glob('**/*.wav', recursive=True)
for wf in wav_files:
    file_path = os.path.splitext(wf)[0]
    file_head = os.path.split(file_path)[-1]
    try:
        move('./{}.mp3'.format(file_head), 
             '{}.mp3'.format(file_path))
    except:
        print('Could not find or move file {}.mp3, it may not exist.'.format(file_head))

What I understand is that you want the same directory structure for mp3 as for vaw.

You can:

  • browse the directory structure of vaw file and construct a mapping between base names (file names without extension) and relative path.

  • browse the directory structure, searching the mp3 files and find each relative path in the mapping, creating the target directory structure if missing and move the file in.

For instance:

import os

vaw_dir = 'path/to/MyVaw' # parent of Folder1...
musics = {}
for root, dirnames, filenames in os.walk(vaw_dir):
    for filename in filenames:
        basename, ext = os.path.splitext(filename)
        if ext.lower() == '.wav':
            relpath = os.path.relpath(root, vaw_dir)
            print('indexing "{0}" to "{1}"...'.format(filename, relpath))
            musics[basename] = relpath
        else:
            print('skiping "{0}"...'.format(filename))

mp3_dir = 'path/to/MyMp3'
out_dir = vaw_dir  # or somewhere else
for root, dirnames, filenames in os.walk(vaw_dir):
    for filename in filenames:
        basename, ext = os.path.splitext(filename)
        if ext.lower() == '.mp3' and basename in musics:
            relpath = musics[basename]
            path = os.path.join(out_dir, relpath)
            if not os.path.exists(path):
                print('creating directory "{0}"...'.format(path))
                os.makedirs(path)
            src_path = os.path.join(root, filename)
            dst_path = os.path.join(path, filename)
            if src_path != dst_path:
                print('moving "{0}" to "{1}"...'.format(filename, relpath))
                os.rename(src_path, dst_path)
        else:
            print('skiping "{0}"...'.format(filename))
print("Done.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM