Jan-06-2019, 10:03 PM
Hello,
I'm very new to Python, and I've been given some code for a study I'm working on, but I've been having some trouble getting it to run through. Right now things are set so that a .csv file is created with the headers for all the data I'll be collecting. There seems to be a problem with one of the last lines where Python is told to record the data. Included below is the code I've got for setting up the data file (thanks to those who helped me fix some issues with this first bit):
Thanks in advance.
I'm very new to Python, and I've been given some code for a study I'm working on, but I've been having some trouble getting it to run through. Right now things are set so that a .csv file is created with the headers for all the data I'll be collecting. There seems to be a problem with one of the last lines where Python is told to record the data. Included below is the code I've got for setting up the data file (thanks to those who helped me fix some issues with this first bit):
#initialise data file - refer to 'startInfo' above for participant number dataHeader=["Participant","Age","Gender","Handedness","Trial","Cue Side", "SOA","Target Type","RT"] with open("mydata.csv", "w") as dataFile: dataFile.write(",".join(dataHeader) + "\n") #Create function to write data def recordData(pt,age,gender,hand,trialnum,cue,soa,val,rt): dataFile.write(",".join([str(pt),str(age),str(gender),str(hand),str(trialnum),str(cue),str(soa),str(val),str(rt)])+"\n") dataFile.flush()And here is the latter part of the code that seems to be giving me trouble:
#loop for trial sequence for i in range (numtrials): fixscreen() if cueside[i]==0: lcue() else: rcue() if soa[i]==0: short_soa() else: long_soa() if validity[i]==0: ltarg() else: rtarg() RTClock=core.Clock() keypress=event.waitKeys(keyList=["space","q"]) RT=RTClock.getTime() if keypress[0][0]=="q": core.quit() else: recordData(str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),i,cueside[i],soa[i],validity[i],RT)Here is the error message I get when trying to run the study:
Error:Traceback (most recent call last):
File "C:\Users\Carol\Documents\COVAT3.py", line 190, in <module>
recordData(str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),i,cueside[i],soa[i],validity[i],RT)
File "C:\Users\Carol\Documents\COVAT3.py", line 35, in recordData
dataFile.write(",".join([str(pt),str(age),str(gender),str(hand),str(trialnum),str(cue),str(soa),str(val),str(rt)])+"\n")
ValueError: I/O operation on closed file.
If anybody could advise me on what's wrong here I'd really appreciate it. Thanks in advance.