0

I have the following function to read files from a directory.

def collect_doc(root, verboose = True):
    filepaths = [os.path.join(root, i) for i in os.listdir(root)]
    if verboose: 
        print(f"filepaths={filepaths}")
    return filepaths

It works but I get the following:

filepaths=['C:/Users/Admin/Downloads/corpus\\0.txt', 'C:/Users/Admin/Downloads/corpus\\1.txt']

I am looking to get the same but as following:

 filepaths=['C:/Users/Admin/Downloads/corpus/0.txt', 'C:/Users/Admin/Downloads/corpus/1.txt']
bib
  • 944
  • 3
  • 15
  • 32
  • 1
    AFAIK, it doesn't really matter, python treats backslashes and forward-slashes the same in Windows paths. – Pranav Hosangadi Apr 25 '22 at 20:52
  • 1
    `os.path.join` will join with your systems `os.sep`. So it seems like you're on a Windows machine but passing in a Linux style path for root. – Chrispresso Apr 25 '22 at 20:53
  • https://stackoverflow.com/questions/12086224/why-not-os-path-join-use-os-path-sep-or-os-sep – Pranav Hosangadi Apr 25 '22 at 20:53
  • Both of those will resolve to the same filenames. Windows now (mostly) tolerates forward slashes in filenames even though backslashes are required. As @Chispresso says, the inconsistency you see is probably because `root` had nonstandard forward slashes in it. – BoarGules Apr 25 '22 at 21:03
  • try `r'C:\Users\Admin\Downloads\corpus\0.txt'` and see what you get :) – BeRT2me Apr 25 '22 at 21:07
  • If you are looking for a tidy output then ```.replace('\\','/')``` – InhirCode Apr 25 '22 at 21:29

0 Answers0