从对这个问题的评论:
是的,只要文件的路径有效,就可以运行此脚本。我用如下小改动改写了您的脚本:
import sqlite3
import os.path
try:
file_name = raw_input("Enter File Path? ")
except:
file_name = input("Enter File Path? ")
if os.path.isfile(file_name):
con = sqlite3.connect(file_name)
cursor = con.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
print(cursor.fetchall())
else:
print("File does not exist")
用法:
> python sqlite_tables.py
(prompt)> Enter File Path? random/file/path
(prompt)> File does not exist
> python sqlite_tables.py
(prompt)> Enter File Path? /valid/path/to/database.db
(prompt)> [(u'django_migrations',), (u'sqlite_sequence',)...]
> python sqlite_tables.py
(prompt)> Enter File Path? ../database.db # relative path
(prompt)> [(u'django_migrations',), (u'sqlite_sequence',)...]