Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between running code in shell and from file

Tags:

python

csv

I'm new to the csv module (and Python in general). I was running some test code to write a CSV file and I realized that it functions when I run it in the shell but not when I run it from a file in IDLE. When I try the latter, it tells me that "open" is invalid syntax. What's going on here?

import csv
with open('csvtest.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow('test1', 'test2', 'test3')
like image 873
CLG Avatar asked Jul 16 '26 04:07

CLG


1 Answers

Quoth the documentation

New in version 2.5.

The with statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers).

Your IDLE is probably running Python 2.4 or earlier.

like image 166
msw Avatar answered Jul 18 '26 16:07

msw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!