NoneType' object has no attribute 'write'

1,959 views
Skip to first unread message

m.t....@student.rug.nl

unread,
Feb 23, 2016, 9:54:28 AM2/23/16
to psychopy-users
Hello everyone,

I am struggling with my python coding a little bit.

Having this code:


-----------------

import csv
import sys
import datetime

log_file = None

# Writes a complete line to the log file and moves to the next line of the log file
def log_write_line(*args):

    # Make the global log_file variable accessible from this function using the 'global' keyword
    global log_file

    # Convert the parameters into a list of parameters
    #args = ['my_block_nr', 'my_trial_nr',1]
    args = list(args)

    # Need to make sure that every single items in the 'args' list is of the variable type 'string'
    # In order for the generic 'join' method below to work. It accepts strings, not integers.
    # therefore 'list comprehensions' is necessary.

    # Read the line below as: 'args' becomes a list of every item 'a' converted to a string
    args = [str(a) for a in args]


    # Join those parameters together to a single comma-separated line.
    # At the end of these joined values, add a new line ("\n") character to move to the next line
    # in my log file.

    log_line = ','.join(args) + "\n"
    print 'log line:', log_line

    # Actually write the composed log line to the log file
    log_file.write(log_line)

    # Flush the data written to the file directly to the disk
    log_file.flush()

# Writes a single value to the log file, followed by a comma- choose sooner
def log_write_value(value):

    # Make the global log_file variable accessible from this function using the 'global' keyword
    global log_file

    print 'log write value:', value, type(value)

    log_file.write(str(value))
    log_file.write(',')
    log_file.flush()

# Moves the output of the log file to the next line
def log_write_newline():
    # Make the global log_file variable accessible from this function using the 'global' keyword

    global log_file
    log_file.write('\n')
    log_file.flush()



# Open new Data file

global log_file

filename = datetime.datetime.now().strftime("experiment-%y%m%d_%H%M%S.csv")

    


# Open Data file with thought probes


def open_data_thought_probes_plus_new_file():

    
    f = open('DATA_File.csv','rU')

    csv_f = csv.reader(f)
    
   
    for row in csv_f:
        if (row[4] == 'none'):

            # thought_probes relevant:
            print row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]

            log_file = open (filename,"w")
            log_write_value(row[6])

            log_file.close()


    f.close()





def calculation_insertion ():
    open_data_thought_probes_plus_new_file()


calculation_insertion()



---------------------

I always receive the error:

NoneType' object has no attribute 'write'



What does that mean and how can I solve this?



Thanks a lot for answering!!


Best,

Marco

Jeremy Gray

unread,
Feb 23, 2016, 11:14:24 AM2/23/16
to psychop...@googlegroups.com
If you look at the whole error message, it should also provide a line number and context about where this is happening. That info is *incredibly* useful. "NoneType' object has no attribute 'write'" is only a part of the error message, not the whole thing.

--Jeremy

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/572dfb55-5f9c-4f1d-ae21-75a2036e81eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

m.t....@student.rug.nl

unread,
Feb 23, 2016, 11:58:16 AM2/23/16
to psychopy-users
Hi Jeremy,

It refers to line 46.


Thanks very much for helping me,

Marco

m.t....@student.rug.nl

unread,
Feb 23, 2016, 12:00:12 PM2/23/16
to psychopy-users
PS: which is log_file.write(str(value))

Jeremy Gray

unread,
Feb 23, 2016, 12:07:32 PM2/23/16
to psychop...@googlegroups.com

PS: which is log_file.write(str(value))

Cool. In general you can copy and paste the whole error, from Traceback... to the end. 

So the error means that the var log_file had the value None when you tried to call its .write() method. None does not have a .write() method. I don't see anywhere in the code that sets log_file to something that might have a method, something like:

log_file = open(filename, 'w+')  # open, + means for appending

I only see log_file = None, and a global log_file declaration (incidentally, a declaration as global is not needed at the module level, only needed within functions or methods if you are assigning to log_file).

--Jeremy

m.t....@student.rug.nl

unread,
Feb 23, 2016, 12:27:20 PM2/23/16
to psychopy-users
Dear Jeremy,

Thanks so much for your help.
It helped a lot.

I wish you a great evening and all the best!

Marco

Jeremy Gray

unread,
Feb 23, 2016, 12:43:16 PM2/23/16
to psychop...@googlegroups.com
Cool, good luck with your research.

--Jeremy

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.

Egle, M.T.

unread,
Feb 23, 2016, 12:47:35 PM2/23/16
to psychop...@googlegroups.com
thank you!

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/yNXripLawZM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.

To post to this group, send email to psychop...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages