Python Forum
reading directory paths from file to list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: reading directory paths from file to list (/thread-13475.html)



reading directory paths from file to list - malonn - Oct-16-2018

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)



RE: reading directory paths from file to list - Larz60+ - Oct-16-2018

check out os.fspath it's made for saving filenames and paths: https://docs.python.org/3/library/os.html


RE: reading directory paths from file to list - malonn - Oct-17-2018

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


RE: reading directory paths from file to list - Larz60+ - Oct-17-2018

newpath = os.fspath(path)



RE: reading directory paths from file to list - snippsat - Oct-17-2018

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.


RE: reading directory paths from file to list - malonn - Oct-26-2018

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...


RE: reading directory paths from file to list - malonn - Oct-26-2018

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