I'm trying to write to a CSV file using Python. Problem is - it looks like it's writing to some hidden object in memory and never outputting to a file.
Here's the code I was trying to use (found here):
import csv
with open('employee_file.csv', mode='w') as employee_file:
employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
employee_writer.writerow(['John Smith', 'Accounting', 'November'])
employee_writer.writerow(['Erica Meyers', 'IT', 'March'])
When I do this, no file actually gets output (as shown in my Windows directory), but I also face no errors. Then, if I try something like the following:
with open('employee_file.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
print(row)
It actually outputs what I had written prior (John Smith
, Accounting
, etc). So it seems like it's being written to some hidden file in memory. I found this thread which suggested using flush()
and close()
to actually write to disk, but neither of these did anything.
Edit: Also checked os.getcwd()
and I am in the correct directory.
Edit 2: Also checked employee_file.closed
and it returns False
in the with open
section and True
after, which seems to work as it should.
Edit 3: One more thing I tried, print(os.path.isfile([absolute path]))
printed True
. So it seems like the file is 100% there in the eyes of Python for some reason.
Edit 4: When I write the exact same code in the python console, it works perfectly fine and outputs a file. Sadly, I have a bit too much modification I need to do that I can't really do this.