0

In the following code, dirpath starts from current directory something like ./x/y. Then I want to add fullname of current directory to it, but it results /root/./x/y/file.txt, but I want /root/x/y/file.txt

def get_files(self):
        cur_path = Path().resolve()
        print("cur_path:", cur_path)
        for dirpath, dirnames, filenames in os.walk("."):
            for filename in [f for f in filenames if "scored_" in f]:
                dfname = os.path.join(cur_path, dirpath, filename)
                tag = dfname.replace("/","_")
                print("dfname:", dfname)
                self.dflist[tag.strip()] = dfname.strip()
Ahmad
  • 8,811
  • 11
  • 76
  • 141

2 Answers2

1

You can use PurePath

from pathlib import PurePath

PurePath('/root','./x/y','filename')

returns:

PurePosixPath('/root/x/y/filename')
Mohammad
  • 3,276
  • 2
  • 19
  • 35
1

Please try this,

import os
def get_files(self):
        cur_path = Path().resolve()
        print("cur_path:", cur_path)
        for dirpath, dirnames, filenames in os.walk("."):
            for filename in [f for f in filenames if "scored_" in f]:
                #dfname = os.path.join(cur_path, dirpath, filename)
                #Used normpath to remove dot while joining current directory & directory path
                dfname=os.path.normpath(os.path.join(cur_path, dirpath, filename))
                tag = dfname.replace("/","_")
                print("dfname:", dfname)
                self.dflist[tag.strip()] = dfname.strip()
Mohan
  • 107
  • 2