Help a noob with Python

This is the place for queries that don't fit in any of the other categories.

Help a noob with Python

Postby Ghost01 » Thu May 08, 2014 11:55 pm


I was hoping you guys could help me out. I got a code from a developer that allowed me to refer to a folder containing a bunch of TXT files that symbolizes a participant each. These files would then be filtered into some filters that I provide in a seperate file, which also contains the address for the input folder and output folder. The script then creates a file for each of the TXT files in a folder called after the designated filter. And it creates a complete folder that merges all the files together for each filter.

What I want now instead is a lot more simple. I want a script to take each TXT file, and filter it accordingly to the filters I provide, and only creating one folder that has all the filters together for each participant and still be tab deliminated TXT file. Whats more is that I noticed in the previous script, that it adds a heading, but that only occurs for the first participant. But I want it to occur for each of the files.

I hope you guys can help me with my conundrum.

Code: Select all
import os, os.path, sys

def filterSub(inputFolder, outputFolder, filters):
    headder = "StudyName   ExportDate   Name   Age   Gender   StimulusName   SlideType   EventSource   Timestamp   MediaTime   PostMarker   Annotation   Epoc (Raw)   Offset (Raw)   SDKTimeStamp (Raw)   ECG (Raw)   POz (Raw)   Fz (Raw)   Cz (Raw)   C3 (Raw)   C4 (Raw)   F3 (Raw)   F4 (Raw)   P3 (Raw)   P4 (Raw)   Epoc (Decon)   Offset (Decon)   SDKTimeStamp (Decon)   ECG (Decon)   POz (Decon)   Fz (Decon)   Cz (Decon)   C3 (Decon)   C4 (Decon)   F3 (Decon)   F4 (Decon)   P3 (Decon)   P4 (Decon)   Epoc   SDKTimeStamp   Classification   HighEngagement   LowEngagement   Distraction   Drowsy   WorkloadFBDS   WorkloadBDS   WorkloadAverage   LiveMarker   KeyStroke   MarkerText"
    files = []
    if not os.path.exists(inputFolder):
        print "Cannot find the path specified. The file/path does not exist."
        return False

    if os.path.isfile(inputFolder):
        print "Error: Input Is a file and not a folder"
        return False
        files = [f for f in os.listdir(inputFolder) if os.path.isfile(os.path.join(inputFolder,f))]
    path = os.path.dirname(inputFolder)
    outputdir = outputFolder
    if not os.path.exists(outputdir):

    ext = ".txt"

    outputDirs = []
    fcombinedOutputs = []
    combinedOutput = os.path.join(outputdir, "Complete")
    if not os.path.exists(combinedOutput):
    for x in filters:
        outputDirs = outputDirs + [os.path.join(outputdir, x)]
        if not os.path.exists(outputDirs[-1]):
        fcombinedOutputs = fcombinedOutputs + [open(os.path.join(combinedOutput, x+ext), 'w')]
    for f in fcombinedOutputs:
    numFiles = 0
    for fname in files:
        if fname[-4:] != '.txt' and fname[-4:] != '.csv':
        numFiles = numFiles+1
        print "Processing", fname
        print "Filtering..."
        finput = open(os.path.join(inputFolder,fname))
        foutputs = []
        for x in outputDirs:
            foutputs = foutputs + [open(os.path.join(x, fname[:-4]+ext), 'w')]
            if numFiles == 1:

        #foutputSTDS = open(os.path.join(outputdir, fname[:-4]+'-convSTDS'+ext), 'w')
        lines = finput.readlines()
        printed = False
        for index in xrange(6,len(lines)):
            record = lines[index].split('\t')
            #print record[6]
            if record[6] != "TestImage":
            for x in range(len(filters)):
                #print record[5]
                if record[5] == filters[x]:
            n = n+1
        for f in foutputs:
        print "Successfully converted: "+str(n)+" lines."
    print "Completed processing "+str(numFiles)+" files."
    for f in fcombinedOutputs:

print "Testing"

# Allow use as a module or standalone script
if __name__ == "__main__":
    print "Begining Filter Sctipt."
    print "Reading Configuration File"
    setupFile = open("setup.txt")
    config = setupFile.readlines()
    filterParams = []
    for x in range(7,len(config)):
        filterParams = filterParams + [config[x].strip()]
    raw_input("Script Finished. Press Return to exit. ")
Last edited by Mekire on Fri May 09, 2014 8:24 am, edited 2 times in total.
Reason: First post lock.
Posts: 1
Joined: Thu May 08, 2014 11:42 pm

Return to General Coding Help

Who is online

Users browsing this forum: Yahoo [Bot] and 12 guests