python找不到csv文件_Python CSVWriter正在创建我的目录中似乎不存在的CSV文件

1586010002-jmsa.png

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.

解决方案

Well, I found a workaround... though not a very clean approach.

Since I discovered that it was exporting perfectly fine using the Python console, I just enclosed my data processing in a function (which returns a list of rows) inside of a file called csvautomate.py. Then I went into the console and did the following:

import csvautomate

with open("output.csv", mode='w', newline="") as f:

writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

for row in csvautomate.processdata():

writer.writerow(row)

And it worked as intended, outputting a correctly formatted file called output.csv.

However this isn't really an ideal solution, so I'm curious if anyone has any other options. Seems when I run files in the cmd using py file.py it doesn't work as intended for some reason. When I use 64-bit python (I'm on Win7 64-bit) I get this file export problem. When I use 32-bit python I get a "python37.dll is missing" error.

Edit: Solved the 'python37.dll is missing" error with 32-bit python by choosing a new install path (just made a folder on my desktop for the time being). Looks like it was probably some kind of permissions issue with the previous location. However, it didn't fix the problem with not writing files.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值