Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Parsing and grouping filenames in directory
    primarykey
    data
    text
    <p>I'm pretty new to python, but I have lots of experience with MATLAB &amp; C. </p> <p>What I need to do it parse the filenames of files in a particular directory, separate them into groups according to the fields within the file names, and perform operations within these groups. </p> <p>Specifically, the filenames are:</p> <pre><code>PROJECT-x-SUBJECT-x-SESSION-x-TYPE.extension </code></pre> <p>where that '-x-' has been purposely inserted as the field divider. I need to do operations on every group of files that shares the same PROJECT-x-SUBJECT-x-SESSION component. </p> <p><em><strong></em>__<em>_</em>____</strong><em>My best attempt follows: <strong></em>__<em>_</em>__<em>_</em>__</strong></p> <p>I can parse each of the files one at a time by:</p> <pre><code>dirList=os.listdir(directory) for fname in dirList: # kill extension ext = os.path.splitext(fname) # get the 4 fields labels=ext[0].split('-x-') PROJECT_list.append(labels[0]) SUBJECT_list.append(labels[1]) ... </code></pre> <p>... which reflects this only idea I have had on how to organize this stuff: by creating 4 lists and appending to them for each filename. </p> <p>Then with my 4 (ordered?) lists, I could then call something like:</p> <pre><code>from collections import Counter c=Counter(SESSION_list) list(c) </code></pre> <p><em>Then at least I have a unique list of SESSION names</em></p> <p>Suggestions? I could go on, but since I really just need a starting point, I think that this is sufficient. </p> <p>Thanks, guys.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload