gpt4 book ai didi

mysql - IOError 没有这样的文件或目录,但文件确实存在

转载 作者:行者123 更新时间:2023-11-29 11:26:14 24 4
gpt4 key购买 nike

以下程序用于读取 CSV 文件并将其转换为 MySQL 插入语句:

import csv

openFile = open('test.csv', 'r')
csvFile = csv.reader(openFile)
header = next(csvFile)
headers = map((lambda x: "'"+x+"'"), header)
insert = 'INSERT INTO Table (' + ", ".join(headers) +", 'time_stamp'" +") VALUES "
for row in csvFile:
values = map((lambda x: "'"+x.strip()+"'"), row)
print (insert +"("+ ", ".join(values) +", getdate());" )
openFile.close()

我进行了以下修改,试图让程序处理多个文件,而不是仅仅显式列出文件名,但不断收到错误消息 IOError: [Errno 2] No such file or directory: '现有文件的确切名称.csv',当文件明确存在时,它正在打印确切的文件名。如何编辑以下代码以使其适用于目录中的一组文件而不会出现上述错误?

import csv, os

path = 'C:/Users/August/Desktop/TripDetailFiles/test'
for csvFile in os.listdir(path):
if csvFile.endswith('.csv'):
openFile = open(csvFile)
readFile = csv.reader(openFile)
header = next(readFile)
headers = map((lambda x: "'"+x+"'"), header)
insert = 'INSERT INTO Table (' + ", ".join(headers) +", 'time_stamp'" +") VALUES "
for row in csvFile:
values = map((lambda x: "'"+x.strip()+"'"), row)
print (insert +"("+ ", ".join(values) +", getdate());" )
openFile.close()

最佳答案

绝对路径向我表明您没有从该目录内部运行脚本。

我相信 os.listdir 只给你文件名而不是路径,并且这些文件名不存在于脚本运行的目录中。你需要连接路径和文件名!

openFile = open(path + '/' + csvFile)

关于mysql - IOError 没有这样的文件或目录,但文件确实存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38154512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com