Python error - "IOError: File not open for reading"

1,248 views
Skip to first unread message

Hezi Ben Sasson

unread,
Mar 28, 2015, 4:30:12 PM3/28/15
to psychop...@googlegroups.com
Hi guys,

I need some help from python experts. 

I have a csv file with value and image, think:
1 xx.jpg
2 yy.jpg
3 xyx.jpg

In the beginning of the experiment I randomize the order of the list. (I put the good in the "begin experiment" part tab)

import csv
import random

with open('values.csv') as f:
    r = csv.reader(f)
    header= next(r)
    l=list(r)

value = [x[0] for x in l]
random.shuffle(value)

image = [x[1] for x in l]
random.shuffle(image)

with open('random.csv', "wb") as f:
    csv.writer(f).writerows([header] + zip(value, image))


Then, I want to open the randomized csv again to sort the list in ascending order. (I put the good under "Begin Routine" tab)

import csv
import operator
sample=open('random.csv', "wb")

csv1=csv.reader(sample, delimiter='.')
sort= sorted(csv1, key=operator.itemgetter(0))

Psychopy returns this error:   

    sort=sorted(csv1, key=operator.itemgetter(0)) 
IOError: File not open for reading


I thought that by
with open
should close the file automatically, but it doesn't seem to be the case here. What am i missing?

Thanks,
 

Axel Kohler

unread,
Mar 28, 2015, 4:47:08 PM3/28/15
to psychop...@googlegroups.com

Hi,

In the second code part, you open the file in "write binary" mode:


sample
=open('random.csv', "wb")

For reading a file, you should use the mode "rb" (I guess you could leave out the "b" anyway in your case; you are doing text import). Since "r" is the default, you can also just leave out the mode argument, as you did in the first code segment.

Best,

Axel

HBS

unread,
Mar 28, 2015, 6:16:53 PM3/28/15
to psychop...@googlegroups.com
Hi Alex,

Thanks for the quick response. 

(sorry ahead if these are amateur python questions. )

Great, I'm not getting an error. However, I don't see that my file has changed (that is, it wasn't sorted out). Is this because I used csv.reader and now csv.writer? Let's say that I want to save the sorted file, how would I do it?

Thanks again.

----
Hezi Ben Sasson

--
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/H8yc7WT9dcM/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.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/24e2fde9-cb2a-4d52-a10b-0a7028c5804e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michael MacAskill

unread,
Mar 29, 2015, 4:53:28 PM3/29/15
to psychop...@googlegroups.com

> On 29/03/2015, at 11:16, HBS <hbs...@gmail.com> wrote:
>
> However, I don't see that my file has changed (that is, it wasn't sorted out). Is this because I used csv.reader and now csv.writer? Let's say that I want to save the sorted file, how would I do it?

You need to do exactly what you did in your first bit of code: (1) read in some variables from a file, (2) manipulate them, and (3) save them back to a file.

Currently, in your second code snippet you are only doing the first two steps.

Regards,

Michael


HBS

unread,
Mar 29, 2015, 7:27:00 PM3/29/15
to psychop...@googlegroups.com
Thanks, Michael. It's working well now.

Regarding the sorting action - as of now the code creates a list by looking only at the first number: 100, 125, 190, 200, 25, 250, 3...

Is there a way to create a numerical ascending/descending list? I tried using next(csv1, None), but it didn't seem to work.

Thanks,  

----
Hezi Ben Sasson

--
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/H8yc7WT9dcM/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.

Hezi Ben Sasson

unread,
Mar 29, 2015, 8:02:14 PM3/29/15
to psychop...@googlegroups.com
Sorry, the "next(csv1, None)" was referring to another issue I dealt with (but just solved!) in regards how to leave the headers of the list intact before sorting the list.

To solve the numerical list, I've tried to use:
sort=csv1.sort(key=lambda row: row[0], reverse=True)

But it returns an error: AttributeError: '_csv.reader' object has no attribute 'sort'

On Sunday, March 29, 2015 at 7:27:00 PM UTC-4, Hezi Ben Sasson wrote:
Thanks, Michael. It's working well now.

Regarding the sorting action - as of now the code creates a list by looking only at the first number: 100, 125, 190, 200, 25, 250, 3...

Is there a way to create a numerical ascending/descending list? I tried using next(csv1, None), but it didn't seem to work.

Thanks,  

----
Hezi Ben Sasson

On Sun, Mar 29, 2015 at 4:53 PM, Michael MacAskill <michael....@otago.ac.nz> wrote:

> On 29/03/2015, at 11:16, HBS <hbs...@gmail.com> wrote:
>
>  However, I don't see that my file has changed (that is, it wasn't sorted out). Is this because I used csv.reader and now csv.writer? Let's say that I want to save the sorted file, how would I do it?

You need to do exactly what you did in your first bit of code: (1) read in some variables from a file, (2) manipulate them, and (3) save them back to a file.

Currently, in your second code snippet you are only doing the first two steps.

Regards,

Michael


--
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/H8yc7WT9dcM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-users+unsubscribe@googlegroups.com.
To post to this group, send email to psychopy-users@googlegroups.com.

Michael MacAskill

unread,
Mar 29, 2015, 8:06:51 PM3/29/15
to psychop...@googlegroups.com

> Regarding the sorting action - as of now the code creates a list by looking only at the first number: 100, 125, 190, 200, 25, 250, 3...
>
> Is there a way to create a numerical ascending/descending list? I tried using next(csv1, None), but it didn't seem to work.

Dear Hezi,

I'm not familiar with the functions you are using, but I guess the numerical values have by default been read in as strings of alphanumeric characters rather than as actual numbers. Hence they are effectively being sorted alphabetically rather than numerically.

You probably need to explicitly coerce them from a string representation into an actual numerical value, e.g. for integers like these, perhaps use the int() function:

myNumber = int(myNumber)

You may or may not need to coerce the values back into character strings using the str() function when it comes time to write them back to disk.

Regards,

Michael


HBS

unread,
Mar 30, 2015, 12:43:13 AM3/30/15
to psychop...@googlegroups.com
Hi Michael,


Thanks for the help. I was able (with some help from others) to find a solution. I'm posting it here, hopefully other might find it useful one day.

csv_writer.writerows(sorted(csv1, key=lambda x:int(x[0])))


----
Hezi Ben Sasson


Regards,

Michael


--
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/H8yc7WT9dcM/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