1

I am trying to create a simple output file containing a timestamp (of when the stimulus appears) and the colour of the stimuli.

I am able to write a file with just the colours, however whenever I try to create a file with both the timestamp and the colour I get an error. "TypeError: cannot concatenate 'str' and 'datetime.datetime' objects"

Code below:

from psychopy import visual, core
import random
import time
import datetime
import time 
from time import strftime


f = open('2015-07-15-Random-Output.txt', 'w')
print f

file = open ('2015-07-15-Random-Output.txt', 'w')


win = visual.Window([800,800],monitor="testmonitor", units="deg")

HolaMundo = "Hola Mundo"

for frameN in range(10):
    MyColor = random.choice(['red','blue','green','pink','purple','orange','yellow','black','white'])
    time = datetime.datetime.now()
    print time
    data = MyColor + str(time)
    msg = visual.TextStim(win, text=HolaMundo,pos=[-4,0],color=MyColor)
    msg.draw()
    win.flip()
    core.wait(.1)
    datetime.datetime.now
    file.write(time + '\n')


file.close()
Ned Rockson
  • 1,095
  • 7
  • 16
Jessica C
  • 31
  • 1
  • 6

5 Answers5

2

datetime.datetime.now only references the method, but does not call it. It should be str(datetime.datetime.now()) or:

time = datetime.datetime.now()
time.strftime('%m/%d/%Y') #formats the date as a string

More information on formatting here

Referencing a previous question here

Community
  • 1
  • 1
Sandwich Heat
  • 559
  • 1
  • 8
  • 20
0

I think this is your problem:

file.write(time + '\n')

The time variable is not a string. What you want is this:

file.write(data+ '\n')

jramirez
  • 8,537
  • 7
  • 33
  • 46
0

Datetime is an object and it doesn't automatically format itself. You need to use strftime [1] in order to make it look the way you'd like. Based on your filenames, you could set it up via the Year-month-day by doing:

timeString = time.strftime('%Y-%d-%m')

If instead you wanted the number of seconds since epoch you could do

timeString = time.strftime('%s')

Then you should print timeString instead of time when you're writing to file:

file.write(timeString + '\n')

[1] - https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

Ned Rockson
  • 1,095
  • 7
  • 16
0
MyColor = random.choice(['red','blue','green','pink','purple','orange','yellow','black','white'])
time = str(datetime.datetime.now())
data = MyColor + "     " + time
msg = visual.TextStim(win, text=Phrase,pos=[0,4],color=MyColor)
msg.draw()
win.flip()
core.wait(.1)
file.write(data + '\n')

Thanks!appreciate the help, I think i figured it out.

Jessica C
  • 31
  • 1
  • 6
0

To concatenate them you have to do str() of the datetime object. Also:

f = open('2015-07-15-Random-Output.txt', 'w')
print f

file = open ('2015-07-15-Random-Output.txt', 'w')

It's kind of redundant and simpler if you just delete the file = open line and instead do this:

from psychopy import visual, core
import random
import time
import datetime
import time 
from time import strftime


with open('2015-07-15-Random-Output.txt', 'w') as f:
  print f
  win = visual.Window([800,800],monitor="testmonitor", units="deg")
  HolaMundo = "Hola Mundo"

  for frameN in range(10):
    MyColor =   random.choice(['red','blue','green','pink','purple','orange','yellow','black','white'])
    time = datetime.datetime.now()
    print time
    data = MyColor + str(time)
    msg = visual.TextStim(win, text=HolaMundo,pos=[-4,0],color=MyColor)
    msg.draw()
    win.flip()
    core.wait(.1)
    datetime.datetime.now
    f.write(time + '\n')

f.close()
jweiner
  • 1
  • 4