I have to get path of all files from a folder, with a specific extension, let's say .txt ( I thing it's the same for another extension) and put them in a .txt file. The problem is that in the folder, are others folders with other .txt files, and I need the path from them too. I wrote a code, but it's not working for subfolders. Can anybody help?
list1=[];
outputFilePath = 'C:\...\playlist.txt'
with open("C:\\...\\folderPath2.txt","r") as f:
output = f.readline()
for dir_, _, files in os.walk(output):
for fileName in files:
relDir = os.path.abspath(output)
relFile = os.path.join(relDir, fileName)
list1.append(relFile)
with open(outputFilePath, 'r+') as file1:
for lines in list1:
file_name, file_extension = os.path.splitext(lines)
if (file_extension == '.txt'):
file1.writelines(lines)
file1.writelines('\n')