为什么我的Python可执行文件在以管理员身份运行时不能识别文件路径?

2024-06-17 15:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python自动化从Excel到Access的数据传输任务。我已经创建了一个脚本来完成这个任务,但是从中创建的可执行文件有问题。(这个任务需要别人来完成,我只是在布置。)

我已经创建了四个不同版本的可执行文件,它们都可以作为脚本工作:

  1. 使用Excel文件的绝对路径指定Excel文件,并将数据输入Access。

  2. 使用os.path.join(os.path.dirname(os.path.abspath(__file__)), 'filename.xlsx')指定Excel文件,并将数据输入Access。

  3. 与脚本1相同,但数据保存为单独的Excel文件。

  4. 与脚本2相同,但数据保存为单独的Excel文件。

脚本3和脚本4都工作正常。脚本1和脚本2返回与Excel文件路径相关的错误,如下所示。我怀疑这与需要管理员权限的脚本1和脚本2有关,因为我收到的错误消息与原始Excel文件有关,而原始Excel文件保持不变。你知道吗

当我使用Excel文件的绝对位置指定该文件时,可执行文件会出现以下错误:

[Errno2] No such file or directory: 'absolute location'

使用os.path.dirname(os.path.abspath(__file__))获取位置时,出现以下错误:

('HY024', "[HY024] [Microsoft][ODBC Microsoft Access Driver] 'unknown' is not a
valid path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides. (-1023) (SQLDriverConnect);
[HY024] [Microsoft][ODBC Microsoft Access Driver] 'unknown' is not a valid path.
Make sure that the path name is spelled correctly and that you are connected to
the server on which the file resides. (-1023)")

Tags: 文件the数据path脚本可执行文件thataccess