简体   繁体   中英

Python not creating a file(csv or txt)

I am using windows 10 and Python 3.6.5 through Anaconda.

I am trying to create a CSV and then add a list to said CSV but I cannot get it to create the CSV in the first place. No error codes, just nothing. I have disabled all antivirus protection to make sure that wasnt the issue. I have tried on two different PCs. I am sure it is something stupid as I am newish to python, but I cant find the answer.

I have tried two ways.

open("new.txt","w").close

and

import csv

with open('names.csv', "w") as csv_file:
    csv_writer = csv.writer(csv_file, delimiter=",")

    print(csv_writer)

this gives me the memory position of csv_writer as expected but other than that does nothing.

Please help a newbie out! Thanks. I promise I have googled and tried everything I know.

I have tried your code and it works perfectly fine for me. Perhaps try the following to see exactly where you created the file:

import csv, os

current_dir = os.getcwd()

with open('names.csv', "w") as csv_file:
    print('opened csv at {}/names.csv'.format(current_dir))
    csv_writer = csv.writer(csv_file, delimiter=",")
    print(csv_writer)

If you're using Anaconda Jupyter notebook works really well. But this can also be implemented anywhere. Make a new folder. Put the text file you want to open and the Python file in the same folder. Do 'import pandas as pd' in your python file. Then do:

file = pd.read_csv('your_file').

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