Printing file path of lift elements - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Printing file path of lift elements (/thread-35080.html) |
Printing file path of lift elements - dyerlee91 - Sep-27-2021 Say I generated a list of all the files in a specific directory, but instead of just the file names, I wanted to print the complete file path for each element into a .txt file. Is is possible to associate a file path with something that's now included in a list? I'm very much a novice, so apologies if this doesn't make sense. RE: Printing file path of lift elements - snippsat - Sep-27-2021 (Sep-27-2021, 12:44 PM)dyerlee91 Wrote: I'm very much a novice, so apologies if this doesn't make sense.Yes it make sense,but you should give try with some code then you show some effort and easier to help. Here a couple of examples using pathlib import pathlib lst = [] mydir = r'G:\div_code\answer\weather' for file in pathlib.Path(mydir).iterdir(): if file.is_file(): lst.append(file.name) >>> lst ['file_1_0_.txt', 'file_33.txt', 'key.ini', 'open_weather.py', 'to_zip.txt'] >>> >>> for file in lst: ... print(f'{mydir}{file}') ... G:\div_code\answer\weatherfile_1_0_.txt G:\div_code\answer\weatherfile_33.txt G:\div_code\answer\weatherkey.ini G:\div_code\answer\weatheropen_weather.py G:\div_code\answer\weatherto_zip.txtSo over generate absolute path for files in the list. If remove .name then get absolute path with one go.import pathlib lst = [] mydir = r'G:\div_code\answer\weather' for file in pathlib.Path(mydir).iterdir(): if file.is_file(): lst.append(file) >>> lst [WindowsPath('G:/div_code/answer/weather/file_1_0_.txt'), WindowsPath('G:/div_code/answer/weather/file_33.txt'), WindowsPath('G:/div_code/answer/weather/key.ini'), WindowsPath('G:/div_code/answer/weather/open_weather.py'), WindowsPath('G:/div_code/answer/weather/to_zip.txt')] |