简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM