简体   繁体   English

Python ODBC连接:不是有效的文件名错误

[英]Python ODBC connection: not a valid file name error

I am new to Python and have been assigned the task to copy all the MS Access database files(we have five) into CSV format using Python. 我是Python新手,已经分配了使用Python将所有MS Access数据库文件(我们有五个)复制为CSV格式的任务。 I have searched through lots of posts on Stack Overflow and sketched together this amateur snippet. 我搜索了Stack Overflow上的很多帖子,并将这个业余片段勾勒在一起。 I need to see the files I have in my MS Access database. 我需要在MS Access数据库中看到我的文件。 Can someone please provide assistance. 有人可以提供帮助。

Pyodbc Error - Python to MS Access Pyodbc错误 - Python到MS Access

open access file in python 在python中打开访问文件

import pyodbc

conn_string = ("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=T:\\DataDump\\7.18.2016 PCR etrakit.accdb") 

conn = pyodbc.connect(conn_string)

cursor = conn.cursor()

cursor.close()
conn.close()

print 'All done for now'

[UPDATED]Try running this [更新]试试这个

conn_string = ("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\T:\\DataDump\\7.18.2016 PCR etrakit.accdb")

use double backslash instead. 改为使用双反斜杠。

Per this post : 根据这篇文章

Try doing it as a single line 尝试将其作为单行进行

conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};C:\\T:\\DataDump\\7.18.2016 PCR etrakit.accdb;'

However, I am a bit confused by your file path. 但是,我对您的文件路径感到有点困惑。 On the root of your C:\\ drive, you have a directory named T: ? C:\\驱动器的根目录下,有一个名为T:的目录T:

It may also be worth noting that file paths with spaces in the name are not always handled as expected. 值得注意的是,名称中带有空格的文件路径并不总是按预期处理。 An alternate approach would be to try and escape the spaces in your file path: 另一种方法是尝试并转义文件路径中的空格:

conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};C:\\T:\\DataDump\\7.18.2016\ PCR\ etrakit.accdb;'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM