Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare folder contents

Tags:

python

I need to compare two folders on a XP machine.

This is a radio station, we have all our music stored as high bitrate mp3, when new songs are acquired from CD they are wav. I need to be able to compare the mp3 and the wav folders for duplicates (naming will be identical except for the file extension). The object is to produce a list of items in the wav folder that don't have mp3 versions.

Python 2.7 is installed and my very limited experience of coding has been with python.

All help appreciated, even if it is just a kick in the right direction... Thanks.


2 Answers

Use os.listdir to get the folder contents, and os.path.splitext to determine the base name:

import os
wavs = set(os.path.splitext(fn)[0] for fn in os.listdir('/path/to/wavs'))
mp3s = set(os.path.splitext(fn)[0] for fn in os.listdir('/path/to/mp3s'))
must_convert = wavs - mp3s

If you want to collate the mp3s and wavs of multiple folders (but not recursively), you'll have to store both basename and the full filename:

import os,collections
files = collections.defaultdict(dict)
for d in ['/path/to/wavs', '/more/wavs', '/some/mp3s', '/other/mp3s']:
    for f in os.listdir(d):
        basename,ext = os.path.splitext(f)
        files[ext][basename] = os.path.join(d, f)
files_to_convert = [fn for basename,fn in files['.wav'].items()
                       if basename not in files['.mp3']]
like image 51
phihag Avatar answered Mar 29 '26 03:03

phihag


    import os
    wav=[os.path.splitext(x)[0] for x in os.listdir(r'C:\Music\wav') if os.path.splitext(x)[1]=='.wav']
    mp3=[os.path.splitext(x)[0] for x in os.listdir(r'C:\Music\mp3') if os.path.splitext(x)[1]=='.mp3']  

   #here wav is a list names of only those files whose extension is .wav 
   #here mp3 is a list names of only those files whose extension is .mp3 

    print(set(wav)-set(mp3))
like image 43
Ashwini Chaudhary Avatar answered Mar 29 '26 04:03

Ashwini Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!