os.path.isfile(os.path.abspath("adb.exe")) Return 错误
os.path.isfile(os.path.abspath("adb.exe")) Return false
为什么 os.path.isfile(os.path.abspath("adb.exe"))
return 是假的?
我在与 os.path.isfile(os.path.abspath("fastboot.exe"))
相同的脚本目录中有 adb.exe
但是当我将它们复制到我的 python 目录,然后开始交互 shell,例如。 os.path.isfile(os.path.abspath("adb.exe"))
给我 True。
或者有更好的方法来验证脚本目录中是否存在文件。
原因 os.path.isfile(os.path.abspath("adb.exe"))
returns False
是因为当你 运行 脚本时, python 实际上是 运行ning 从它的安装目录,不是脚本位置。
如果您想获取当前文件夹路径,您可以使用:
os.path.dirname(os.path.abspath(__file__))
或@blakev 建议:
os.path.split(__file__)[0]
因此您可以使用以下方法检查脚本位置是否存在文件:
os.path.isfile(os.path.dirname(os.path.abspath(__file__)) + os.sep + filename)
注:
os.sep
包含操作系统文件系统使用的适当分隔符,因此 \
在 Windows 系统上。
__file__
仅在 运行 脚本文件时存在,它不会只在命令行中 运行 宁 python 工作。它 return 是脚本的完整路径和名称。例如,运行在我的桌面上安装一个名为 script.py 的脚本,它可能 return C:\Users\Nick A\Desktop\script.py
为什么 os.path.isfile(os.path.abspath("adb.exe"))
return 是假的?
我在与 os.path.isfile(os.path.abspath("fastboot.exe"))
相同的脚本目录中有 adb.exe
但是当我将它们复制到我的 python 目录,然后开始交互 shell,例如。 os.path.isfile(os.path.abspath("adb.exe"))
给我 True。
或者有更好的方法来验证脚本目录中是否存在文件。
原因 os.path.isfile(os.path.abspath("adb.exe"))
returns False
是因为当你 运行 脚本时, python 实际上是 运行ning 从它的安装目录,不是脚本位置。
如果您想获取当前文件夹路径,您可以使用:
os.path.dirname(os.path.abspath(__file__))
或@blakev 建议:
os.path.split(__file__)[0]
因此您可以使用以下方法检查脚本位置是否存在文件:
os.path.isfile(os.path.dirname(os.path.abspath(__file__)) + os.sep + filename)
注:
os.sep
包含操作系统文件系统使用的适当分隔符,因此 \
在 Windows 系统上。
__file__
仅在 运行 脚本文件时存在,它不会只在命令行中 运行 宁 python 工作。它 return 是脚本的完整路径和名称。例如,运行在我的桌面上安装一个名为 script.py 的脚本,它可能 return C:\Users\Nick A\Desktop\script.py