NiocTib
NiocTib

Reputation: 90

Cannot create csv file from python in visual studio code

I am a beginner learning programming through CS50, currently on week 7 SQL.

I am trying to apply some basic principles on my own machine instead of CS50 IDE on AWS to make sure I can use other environments.

I use visual studio code on windows, and I am currently struggling on some simple lines of code, which are working on the IDE environment but not on my machine. I am simply trying to create a csv file from python, but when I run my code it executes but no csv file is created. Here is my code:

import csv

with open('mycsv.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(['col1', 'col2', 'col3'])
    writer.writerow(['one', 'two', 'three'])

Any ideas ?

Upvotes: 2

Views: 2765

Answers (2)

CVilla90
CVilla90

Reputation: 13

If you're working in a virtual environment, the script is creating the file in a different folder, look for the folders above.

Upvotes: 0

Matthijs990
Matthijs990

Reputation: 629

maybe try w+ instead of w? Like this:

import csv

with open('mycsv.csv', 'w+') as f:
    writer = csv.writer(f)
    writer.writerow(['col1', 'col2', 'col3'])
    writer.writerow(['one', 'two', 'three'])`

w+ forces python to create a new file if that file doesn't exist yet.

Upvotes: 1

Related Questions