Python Forum
reading directory paths from file to list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading directory paths from file to list
#1
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)
Reply
#2
check out os.fspath it's made for saving filenames and paths: https://docs.python.org/3/library/os.html
Reply
#3
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
Reply
#4
newpath = os.fspath(path)
Reply
#5
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.
Reply
#6
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...
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 397 Jan-28-2024, 10:37 PM
Last Post: kittyticker
Sad problems with reading csv file. MassiJames 3 559 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 626 Oct-29-2023, 12:40 PM
Last Post: Mark17
  trouble reading string/module from excel as a list popular_dog 0 384 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Reading a file name fron a folder on my desktop Fiona 4 851 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,071 Jun-27-2023, 01:17 PM
Last Post: diver999
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 927 Jan-23-2023, 04:56 AM
Last Post: deanhystad
  Reading a file JonWayn 3 1,057 Dec-30-2022, 10:18 AM
Last Post: ibreeden
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,062 Dec-15-2022, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020