【问题标题】:Misunderstanding of python os.path.abspathpython os.path.abspath的误解
【发布时间】:2014-09-02 13:09:31
【问题描述】:

我有以下代码:

directory = r'D:\images'
for file in os.listdir(directory):
    print(os.path.abspath(file))

我想要下一个输出:

  • D:\images\img1.jpg
  • D:\images\img2.jpg 等

但我得到不同的结果:

  • D:\code\img1.jpg
  • D:\code\img2.jpg

其中 D:\code 是我当前的工作目录,这个结果与

os.path.normpath(os.path.join(os.getcwd(), file))

所以,问题是:我必须使用 os.path.abspath 的目的是什么

os.path.normpath(os.path.join(directory, file))

获取我的文件的真实绝对路径?尽可能展示真实的用例。

【问题讨论】:

    标签: python path os.path


    【解决方案1】:

    问题在于您对os.listdir() 不是os.path.abspath() 的理解。 os.listdir() 返回目录中每个文件的名称。这会给你:

    img1.jpg
    img2.jpg
    ...
    

    当您将这些传递给os.path.abspath() 时,它们被视为相对路径。这意味着它与您执行代码的目录相关。这就是你得到“D:\code\img1.jpg”的原因。

    相反,您要做的是将文件名与您列出的目录路径连接起来。

    os.path.abspath(os.path.join(directory, file))
    

    【讨论】:

      【解决方案2】:

      listdir 在目录中生成文件名in,而不引用目录本身的名称。没有任何其他信息,abspath 只能从它可以知道的唯一目录形成绝对路径:当前工作目录。您始终可以在循环之前更改工作目录:

      os.chdir(directory)
      for f in os.listdir('.'):
          print(os.path.abspath(f))
      

      【讨论】:

        【解决方案3】:

        Python 的原生 os.listdiros.path 函数非常低级。遍历一个目录(或一系列降级目录)需要您的程序手动组装文件路径。定义一个实用函数来生成您只需要一次的路径会很方便,这样路径组装逻辑就不必在每次目录迭代中重复。例如:

        import os
        
        def better_listdir(dirpath):
            """
            Generator yielding (filename, filepath) tuples for every
            file in the given directory path.
            """
            # First clean up dirpath to absolutize relative paths and
            # symbolic path names (e.g. `.`, `..`, and `~`)
            dirpath = os.path.abspath(os.path.expanduser(dirpath))
        
            # List out (filename, filepath) tuples
            for filename in os.listdir(dirpath):
                yield (filename, os.path.join(dirpath, filename))
        
        if __name__ == '__main__':
            for fname, fpath in better_listdir('~'):
                print fname, '->', fpath
        

        或者,可以使用“更高级别”的路径模块,例如 py.pathpath.pypathlib(现在是 Python 的标准部分,适用于 3.4 及更高版本,但适用于 2.7向前)。它们为您的项目添加了依赖项,但提升了文件、文件名和文件路径处理的许多方面。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-27
          • 1970-01-01
          • 1970-01-01
          • 2016-11-19
          • 1970-01-01
          • 2021-06-15
          • 2014-02-14
          相关资源
          最近更新 更多