Write a python code that lists all audio files in a directory in seconds.
- User
Write a python code that lists all audio files in a directory in seconds.
- Assistant
import glob
list all mp3 files in a directory. The file extension can be changed to find other types of audio files too such as wav and ogg.
path = "/directory/*.mp3" files = glob.glob(path) print(files)
- User
Can you make a more general code for UNIX systems, where files can have no extensions?
- Assistant
Here's a more general Python script that lists all audio files in a directory, including files with no extensions, on UNIX systems:
import subprocess import os def list_audio_files(directory): audio_files = [] for root, dirs, files in os.walk(directory): for file in files: full_path = os.path.join(root, file) result = subprocess.run(['file', '--mime-type', '-b', full_path], stdout=subprocess.PIPE) mime_type = result.stdout.decode().strip() if mime_type in ['audio/mpeg', 'audio/x-wav', 'audio/mp4', 'audio/x-m4a']: audio_files.append(full_path) return audio_files directory = '/path/to/directory' print(list_audio_files(directory))This script uses the
os.walkfunction to traverse the directory tree and theos.path.joinfunction to construct the full path to each file. It then uses thesubprocessmodule to run thefilecommand, which returns the MIME type of the file. If the MIME type is one of the audio types listed, the script appends the full path to the audio file to theaudio_fileslist. Finally, the function returns the list of audio files. - User
Add documentation to the code
- Assistant
import subprocess import os def list_audio_files(directory): """ Lists all audio files in a directory. :param directory: the directory where files are stored. :returns: the list of files paths """ audio_files = [] for root, dirs, files in os.walk(directory): # os.walk will iterate through all the folders recursively for file in files: full_path = os.path.join(root, file) result = subprocess.run(['file', '--mime-type', '-b', full_path], stdout=subprocess.PIPE) # this calls a system command # returning the MIME type of the file mime_type = result.stdout.decode().strip() # this cleans the MIME type for comparison if mime_type in ['audio/mpeg', 'audio/x-wav', 'audio/mp4', 'audio/x-m4a']: # here are some frequent MIME type for audio files audio_files.append(full_path) return audio_files directory = '/path/to/directory' print(list_audio_files(directory))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →