Lian Arzbecker

Postdoctoral researcher


Curriculum vitae


lian (at) arzbecker (dot) com | lianarzb (at) buffalo (dot) edu


Motor Speech Disorders Lab

Communicative Disorders and Sciences, University at Buffalo



Python scripts


Customizable scripts for simple tasks


Printing the names of all files in a given location
  1. Location of files
  2. Location to save the text file named "files_list.txt"
import os

location = r"«C:\your\location\here»"                   # 01 USER INPUT
output_file = r"«C:\your\location»\files_list.txt"      # 02 USER INPUT

if os.path.exists(location):
    items = os.listdir(location)
    
    with open(output_file, "w", encoding="utf-8") as file:
        file.write(f"Items in '{location}':\n")
        file.writelines(f"{item}\n" for item in items)
    
    print(f"List saved to {output_file}")
else:
    print(f"Error: The location '{location}' does not exist.")
Moving files from one folder to another
1. Path to current location
2. Path to new location
3. Extension(s) of the type of file(s) to move (e.g., docx, pdf)
import shutil, os

source = r"«C:\source\here»"               	 	  # 01 USER INPUT
destination = r"«C:\destination\here»"      	  # 02 USER INPUT
extensions = {".«file»"], ".«type»", ".«here»"}   # 03 USER INPUT

if os.path.exists(source):
    [
        shutil.move(
            os.path.join(source, f), 
            os.path.join(destination, f)
        )  
        for f in os.listdir(source)  
        if os.path.splitext(f)[1].lower() in extensions
    ]
    print("File transfer complete.")
else:
    print(f"Source directory '{source}' does not exist.")
Measuring duration of audio files
  1. Audio file extension(s) to measure (e.g., .wav, .mp3)
  2. Audio folder path(s)
# Get audio duration
# Last updated 03.21.2025 by Lian Arzbecker
# PIP INSTALL TINYTAG

import os
from tinytag import TinyTag
import wave

# Supported audio file extensions
supported_extensions = {'.«audio»', '.«exentions»', '.«here»'}    # 01 USER INPUT

# Folders containing the audio files
folders = [r"«C:\your\folder\here»",                              # 02 USER INPUT
           r"«C:\your\second\folder»"]

def get_audio_duration(file_path: str) -> float:
    """Returns the duration of an audio file in seconds"""
    ext = os.path.splitext(file_path)[1].lower()
    
    # Check for supported file extensions (all in one place)
    if ext in supported_extensions:
        if ext == '.wav':
            with wave.open(file_path, 'rb') as audio_file:
                num_frames = audio_file.getnframes()
                sample_rate = audio_file.getframerate()
                return num_frames / float(sample_rate)
        else:
            try:
                return TinyTag.get(file_path).duration
            except Exception:
                return 0
    return 0

def folder_audio_duration(folder_path: str) -> float:
    """Returns the total duration of supported audio files"""
    return sum(
        get_audio_duration(os.path.join(folder_path, f)) 
        for f in os.listdir(folder_path)
        if os.path.splitext(f)[1].lower() in supported_extensions
    )

def print_audio_duration(folders):
    """Print total duration for each folder in multiple lines."""
    for folder in folders:
        total_duration = folder_audio_duration(folder)
        
        # Calculate hours and minutes
        hours = total_duration // 3600
        remaining_seconds = total_duration % 3600
        minutes = remaining_seconds // 60
        
        # Prepare the strings for printing
        folder_str = f"Total audio duration for folder {folder}:"
        duration_str = f"{total_duration:.2f} seconds"
        time_str = f"({hours:.0f} hours and {minutes:.0f} minutes)"
        
        # Print the final message in multiple lines
        print(folder_str, duration_str, time_str)

# Now call the function
print_audio_duration(folders)

Tools
Translate to