【问题标题】:How to get the full filepath from a filename in python?如何从python中的文件名中获取完整的文件路径?
【发布时间】:2020-02-24 15:46:39
【问题描述】:

我需要通过在 python 2.7 中提供文件名来获取文件的完整路径。 不幸的是,我不允许使用 pathlib 模块。

 Example: 
filename: python.exe
full path that i want to print :  C:\app\tools\python27\python.exe

提前感谢所有需要时间回复的人。

【问题讨论】:

    标签: python-2.7 path filenames filepath os.path


    【解决方案1】:

    查看线程: How do I get the full path of the current file's directory?

    我不确定您是如何在代码中实现这一点的,但是“pathlib”(用于 v3 以上)和“os”(用于 v3 以下)库应该能够提供帮助。

    如果您已经查看了该主题但没有找到答案,我们深表歉意!

    【讨论】:

    • 谢谢,但我已经看过了。不幸的是,正如问题中提到的,我不允许使用 pathlib 模块,python 2.7 的代码: os.path.dirname(os.path.abspath(file)) 只是给了我cwd
    • 没有运气使用“os.path”?
    • 不,只是给我 cwd
    • 不确定,我能想到的唯一方法是使用 os. docs.python.org/2/library/os.path.html。祝你好运找到解决方案:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    相关资源
    最近更新 更多