Oct-16-2018, 10:45 PM
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)