Skip to content Skip to sidebar Skip to footer

Accessing A Folder Containing .wav Files

I am trying to access a folder containing 50 .wav file samples (that I will use for my Gender Voice Classification (using PCA) project), but I cant seem to be able to access the fo

Solution 1:

You cannot access the relative path to your Desktop folder by starting with the absolute path /Desktop/... As a regular user, your desktop folder is stored somewhere deeper -- typically, something like /Users/(yourname)/Desktop. But don't hardcode that, Python can find the home folder for you!

import os
from os.path import expanduser

home = expanduser("~")
print home
raw_folder = home+'/Desktop/soundFile/SoundSamples'
for file in os.listdir(raw_folder):
    print(file)

The OS function expanduser locates your home folder and returns it as a string. Then you can paste on whatever folder you are looking for -- Desktop, Documents, or any other path relative to your home.


Solution 2:

Your filepath should be:

Desktop/soundFile/SoundSamples

(You don't need double forward slashes)

Assuming that you're running it from your home directory, otherwise specify the full filepath:

/Users/your_username/Desktop/soundFile/SoundSamples

(where your_username is your username)

I'd also suggest using the with open(file, 'r') as f: method of reading or writing to files, as Python will automatically close it for you

import os

raw_folder = 'Desktop/soundFile/SoundSamples'
for file in os.listdir(raw_folder):
    print(file)
    filepath = os.path.join(raw_folder, file)
    with open(filepath, 'r') as f:
        print(f.read())

Post a Comment for "Accessing A Folder Containing .wav Files"