Python Forum

Full Version: reading directory paths from file to list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am reading a list saved to a file and want to store it back in a list in code. I can do it, but in the process of saving to a file, Python writes with the backslash escape sequence (which is great) and when I go to put it back in a list it doubles the number of backslashes. So the path is (for example) 'C:\Users\Me\Downloads\Test.txt' and is saved in the file (text file) as 'C:\\Users\\Me\\Downloads\\Test.txt'. Now when I go to read from the text file and store the paths back in a list is doubles the backslashes, like so: 'C:\\\\Users\\\\Me\\\\Downloads\\\\Test.txt'. How can I avoid that? Code:
def scan_dumps(self):
        '''Walks the OS drive and returns a dict with the path to and
        size of each .DMP file found on the drive.'''
        dumps = {}
        k = 0
        for dpath, dname, fname in os.walk('C:\\'):
            for f_n in fname:
                if f_n.endswith('.dmp'):
                    dumps[k] = [os.path.join(dpath, f_n), os.stat(os.path.join(dpath, f_n)).st_size]
                    k += 1
        return dumps

    def write_dumps(self):
        dmp = self.scan_dumps()
        i = 0
        with open('practice_gui_2.ini', 'w') as f:
            while i < len(dmp):
                s = str(dmp[i])
                f.write(f'{s}\n')
                i += 1

    def read_dumps(self):
        with open('practice_gui_2.ini', 'r') as f:
            dumps = []
            for line in f:
                dumps.append(line[2:-2])
        print(dumps)
check out os.fspath it's made for saving filenames and paths: https://docs.python.org/3/library/os.html
Okay, so how would I apply that? I think I would read the text file into a list, but how would I utilize os.fspath to "sanitize" the path's? I haven't coded yet, but off the top of my head I don't see an obvious route.

Thanks,
-malonn
newpath = os.fspath(path)
malonn Wrote:'C:\\\\Users\\\\Me\\\\Downloads\\\\Test.txt'. How can I avoid that? Code:
Other way /,it will work fine.
>>> p = 'C:/Users/Me/Downloads/Test.txt' 
>>> p
'C:/Users/Me/Downloads/Test.txt'
But you most doing something strange to get 4.
If use raw string there is of course 2,but show 1 when print.
>>> p = r'C:\Users\Me\Downloads\Test.txt'
>>> p
'C:\\Users\\Me\\Downloads\\Test.txt'
>>> print(p)
C:\Users\Me\Downloads\Test.txt
Save in and out,there will not be 4 \:
# same for both
# p = r'C:\Users\Me\Downloads\Test.txt'
p = 'C:\\Users\\Me\\Downloads\\Test.txt'
>>> with open('path.txt', 'w') as f_out:
...     f_out.write(p)

>>> with open('path.txt') as f:
...     path = f.read()
    
>>> path
'C:\\Users\\Me\\Downloads\\Test.txt'
>>> print(path)
C:\Users\Me\Downloads\Test.txt
Larz60+ Wrote:check out os.fspath it's made for saving filenames and paths: https://docs.python.org/3/library/os.html
os.fspath() is made to work with pathlib object,also it take pathlib object back to string path.
@malonn dos not use pathlib in post.
I think it has something to do with the conversion to a string?
def write_dumps(self):
        dmp = self.scan_dumps()
        i = 0
        with open('practice_gui_2.ini', 'w') as f:
            while i < len(dmp):
                s = str(dmp[i])
                f.write(f'{s}\n')
                i += 1
Thanks for the help though, Larz60+ and snippsat. I don't know. I took a break and am now getting back to it...
For anyone interested, I found a solution that I like. I decided to use os.path.normpath() on the lines being read off of the file and all is back to normal path-wise.

malonn