#Python code:
import csv # first we need import necessary lib:csv
file=open("dinesh.csv") #prepare a csv file for our example
testWriter = csv.writer(open('dinesh.csv', 'w'), delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamWriter.writerow(['Test'] * 5 + ['Wow'])

Error:-
<type 'exceptions.IOError'>: invalid mode: w
args = ('invalid mode: w',)
errno = None
filename = None
message = 'invalid mode: w'
strerror = None

Recommended Answers

All 4 Replies

message = 'invalid mode: w'

try 'wb'

try 'wb'

not working..invalid mode :(

reading from file is workin.... :((

how to xport data to csv file???

try this

#Python code:
import csv # first we need import necessary lib:csv
file=open("dinesh.csv", "w") #prepare a csv file for our example
testWriter = csv.writer(file, delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
testWriter.writerow(['Test'] * 5 + ['Wow'])
file.close()

I think the error is here (and griboullis solution corrects it)

file=open("dinesh.csv") #prepare a csv file for our example

Here you open the file (in read only mode - the default value)

testWriter = csv.writer(open('dinesh.csv', 'w'), delimiter=' ',

And here, you reopen it when it is not closed... Not good habit ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.