Python cant get full path name of file

后端 未结 2 714
日久生厌
日久生厌 2021-01-04 12:38

Trying to drill through a directory on my drive that has subfoldrs within it. When I find files that have the file extensions I\'m looking for I want the full file path. Rig

相关标签:
2条回答
  • 2021-01-04 13:19

    You probably need to join the filename with the directory that contains it:

    os.path.realpath(os.path.join(root,name))
    

    e.g. I just tested this:

    import os
    for root, dirs, files in os.walk('.'):
        for name in files:
            if name == 'foo':
               name = str(name)
               name = os.path.realpath(os.path.join(root,name))
               print name
    

    with the following directory structure:

    test
      + foo
      + test2
         + foo
    

    and it worked properly.

    0 讨论(0)
  • 2021-01-04 13:34

    Use:

    os.path.abspath
    

    instead. Your path is not absolute.

    0 讨论(0)
提交回复
热议问题