Organize recording files by site

This notebook copies sound files (recordings) into the pumilio directory structure based on a csv file containing information about the time and location of recording.

Required packages

Variable declarations

csv_filepath – path to a csv file containing information about the time and location of each recording
sound_directory – path to the directory that will contain the recordings
source_directory – path to the directory containing the unorganized recordings


In [2]:
csv_filepath = ""

In [3]:
sound_directory = ""

In [102]:
source_directory = os.path.dirname(os.path.dirname(working_directory))

Import statements


In [97]:
import pandas
import os.path
from datetime import datetime
import subprocess

Organize recordings


In [98]:
visits = pandas.read_csv(csv_filepath)

In [99]:
visits = visits.sort_values(by=['Time'])

In [110]:
visits['ID'] = visits['ID'].map('{:g}'.format)

In [111]:
sites = visits['ID'].drop_duplicates().dropna().as_matrix()

In [108]:
for site in sites:
    path = os.path.join(working_directory, str(site))
    if os.path.exists(path):
        os.rmdir(path)
    os.mkdir(path)

In [113]:
for index, visit in visits.iterrows():
    try:
        dt = datetime.strptime(visit['Time'], '%Y-%m-%d %X')
    except TypeError:
        print('Time was not a string, but had a value of: "{0}"'.format(visit['Time']))
        continue
    source_file = os.path.join(source_directory, dt.strftime('%Y-%m-%d'), 'converted', '{0}.flac'.format(dt.strftime('%y%m%d-%H%M%S')))
    destination_file = os.path.join(working_directory, str(visit['ID']), '{0}.flac'.format(dt.strftime('%y%m%d-%H%M%S')))
    if os.path.exists(source_file):
        subprocess.check_output(["cp", source_file, destination_file])
        print('copying {0}'.format(dt.strftime('%y%m%d-%H%M%S')))
    else:
        print('\n')
        print('{0} does not exist!'.format(dt.strftime('%y%m%d-%H%M%S')))
        print(visit['Name'])
        print('\n')
    print('\n')
    print('done')


copying 160127-151500


done
copying 160217-150000


done
copying 160217-153000


done
copying 160217-160000


done
copying 160217-163000


done
copying 160217-170000


done
copying 160218-150000


done
copying 160218-160000


done
copying 160218-164500


done
copying 160218-171500


done
copying 160218-174500


done
copying 160219-143000


done
copying 160219-153000


done
copying 160219-160000


done
copying 160219-164500


done
copying 160222-140000


done
copying 160222-143000


done
copying 160222-150000


done
copying 160222-153000


done
copying 160222-160000


done
copying 160222-163000


done
copying 160223-150000


done
copying 160223-154500


done
copying 160223-161500


done
copying 160223-163000


done
copying 160223-171500


done
copying 160224-150000


done
copying 160224-153000


done
copying 160224-160000


done
copying 160224-163000


done
copying 160301-143000


done
copying 160301-145000


done
copying 160301-153000


done
copying 160301-160000


done
copying 160301-161000


done
copying 160301-164510


done
copying 160302-150000


done
copying 160302-153000


done
copying 160302-160000


done
copying 160302-163000


done
copying 160302-170000


done
copying 160303-150000


done
copying 160303-154500


done
copying 160303-160000


done
copying 160303-164500


done
copying 160303-171500


done
copying 160303-174500


done
copying 160307-134500


done
copying 160307-140000


done
copying 160307-143030


done
copying 160307-150000


done
copying 160307-153000


done
copying 160307-160000


done
copying 160307-163030


done
copying 160308-154500


done
copying 160308-161500


done
copying 160308-163000


done
copying 160308-174500


done
copying 160309-150315


done
copying 160309-160000


done


160311-120000 does not exist!
Sternwarte




done
copying 160314-143600


done
copying 160315-150000


done
copying 160315-174500


done
copying 160316-150000


done
copying 160316-153000


done
copying 160316-160000


done
copying 160316-163000


done
copying 160316-170000


done
copying 160317-145000


done
copying 160317-153000


done
copying 160317-160000


done
copying 160317-161000


done
copying 160317-164500


done
copying 160330-150000


done
copying 160401-143500


done
copying 160404-140000


done
copying 160404-143000


done
copying 160404-150000


done
copying 160404-153000


done
copying 160404-160000


done
copying 160404-163000


done
copying 160405-150000


done
copying 160405-154500


done
copying 160405-160000


done
copying 160405-164500


done
copying 160405-171500


done
copying 160405-174500


done
copying 160406-150000


done
copying 160406-153000


done
copying 160406-160000


done
copying 160406-163000


done
copying 160406-170000


done
copying 160411-140000


done
copying 160411-143000


done
copying 160411-145000


done
copying 160411-153000


done
copying 160411-160000


done
copying 160411-161000


done
copying 160411-164500


done
copying 160412-150000


done
copying 160412-153000


done
copying 160412-160000


done
copying 160412-163000


done
copying 160412-164500


done
copying 160413-150000


done
copying 160413-153000


done
copying 160413-154500


done
copying 160413-161500


done
copying 160413-163000


done
copying 160413-174500


done
copying 160418-140000


done
copying 160418-143000


done
copying 160418-150000


done
copying 160418-153000


done
copying 160418-160000


done
copying 160418-163000


done
copying 160419-150000


done
copying 160419-153000


done
copying 160419-154500


done
copying 160419-161500


done
copying 160419-163000


done
copying 160419-174500


done
copying 160420-153000


done
copying 160420-160000


done
copying 160420-163000


done
copying 160420-170000


done
copying 160421-143000


done
copying 160421-150000


done
copying 160421-153000


done
copying 160421-160000


done
copying 160421-163000


done
copying 160422-143600


done
copying 160425-151500


done
copying 160426-150000


done
copying 160426-154500


done
copying 160426-160000


done
copying 160426-164500


done
copying 160426-171500


done
copying 160426-174500


done
copying 160427-150000


done
copying 160427-153000


done
copying 160427-160200


done
copying 160427-163000


done
copying 160429-140000


done
copying 160429-143000


done
copying 160429-145000


done
copying 160429-153000


done
copying 160429-160000


done
copying 160429-161000


done
copying 160429-164500


done
copying 160502-150000


done
Time was not a string, but had a value of: "nan"