Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Get a consistent path for the file pool (or another way to isolate a subpart of a file name)

edited December 2024 in OpenSesame

In my experiment I want to isolate a part of various sound file names. For instance, if I have the file name Dutch_112.wav , I want to isolate the string 112. I want a way to automize this so that it can apply for various sound files that are called in a sequence. I've been struggling, though, to find a way to call a sound file name without the file pool path. I could simply trim the file path from the name, like the following code shows:

(full sound file path is /var/folders/sg/8vk99wn53bckkgs0j95ydyh0000gn/T/tmph0k3emlj.opensesamepool/Dutch_112.wav ).

sound_file = pool["Dutch_112.wav"]

sound_name = sound_file.replace("/var/folders/sg/8vk99wn53bckkgs0j95ydyh0000gn/T/tmph0k3emlj.opensesamepool/", "")

seq = sound_name.replace("Dutch_", " ")

seq = seq.replace(".wav", " ")

Which gets me just 112.

A problem I'm running into, however, is that every time I reopen my experiment, the file pool path has changed. I have to re-copy-and-paste the file pool path into my replace() code. This is doable, but is there a more efficient way to achieve this? One idea I had was to "set" a consistent file pool directory so the path to trim is always the same, but I can't figure out how to do that (if it's possible). Alternatively there may be a simpler way to get the name of the sound file without the extraneous path. Any ideas?

Comments

  • Hi Jordan,

    You don't need to reinvent the wheel. The os package has many functions to operate with paths. You could do:

    filename = '/var/folders/sg/8vk99wn53bckkgs0j95ydyh0000gn/T/tmph0k3emlj.opensesamepool/Dutch_112.wav'
    import os
    sound_file = os.path.splitext(os.path.basename(filename))[0]
    seq = sound_file.split('_')[1]
    

    Eduard

    Buy Me A Coffee

  • This worked, thank you!

Sign In or Register to comment.