-1

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.

  • 2
    You're probably looking for the file in a completely different directory from where you actually created it. – user2357112 Aug 18 '18 at 18:13
  • Also, turn your antivirus back on! – user2357112 Aug 18 '18 at 18:15
  • Use a full path since your Python is likely using a directory you are not expecting. – dawg Aug 18 '18 at 18:16
  • @user2357112 I turned it back on immediately. – Dylan Airhart Aug 18 '18 at 18:42
  • @dawg that worked. any idea why it wouldnt create in the same folder as the createcsv.py file? Every tutorial I watched it auto did it in that folder. – Dylan Airhart Aug 18 '18 at 18:43
  • _any idea why it wouldnt create in the same folder as the createcsv.py file_ It will create the file in the current directory where you ran python, which is not necessarily the same directory where your script lives. i.e. if you're currently in `/tmp` and you run the command `python /home/users/dylan/createcsv.py`, it will create the file in `/tmp`. – John Gordon Aug 18 '18 at 18:48
  • @JohnGordon thanks, that makes alot of sense. – Dylan Airhart Aug 18 '18 at 18:49

3 Answers3

0

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)
Arnoux
  • 226
  • 2
  • 9
  • Yea, I was for some reason in C:\users\myname instead of the folder my py file was. I was under the impression that it automatically created there from tutorials I watched. Thanks for the current_dir = os.getcwd, that was able to help me figure it out! – Dylan Airhart Aug 18 '18 at 18:48
0

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').
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Beatrice
  • 76
  • 9
0

For me the problem was the format of the name, it won't be created if it contains some special characters like “/“ I removed the special character and it worked fine.

For example : filename = invoice_date_02/2013 → not working filename = invoice_date_02-2013 → work fine