【发布时间】:2022-08-11 23:32:58
【问题描述】:
我的问题是除非我指定完整路径,否则我无法在 VS Code 终端中运行 python 文件。 然而,当我看到任何 python 教程时,只输入了 python 文件名并且它可以工作。 有人可以帮我解决这个问题吗?
-
cd 到代码文件夹并尝试使用文件名运行 python 代码
-
您使用的是什么操作系统?另外,您是否与 Python 文件位于同一目录中?
标签: python
我的问题是除非我指定完整路径,否则我无法在 VS Code 终端中运行 python 文件。 然而,当我看到任何 python 教程时,只输入了 python 文件名并且它可以工作。 有人可以帮我解决这个问题吗?
标签: python
这些文件需要位于您正在操作/或具有其他 Python 文件的同一文件夹中。
为此,您可以使用两种方法:
第一的:
import os
pwd = os.getcwd()
# again, make sure you file is in the same directory.
the_file = (pwd + "\\filename.xlsx")
或其次,
#the below you can use wherever your file is and it will locate it.
# you specify the full path using pathlib.path and:
pathlib.path(directory here without the file itself).joinpath(file name here).
【讨论】: