I've my code like this, but the problem is it only gives the last file name in output.txt instead of all 30 file names.
import os
path="path I wanted"
dirList=os.listdir(path)
for filename in dirList:
print (filename)
f = open("output1.txt", "w")
f.write(filename)
f.close()
The 'w' flag to 'open' truncates the file on every iteration through the loop. If the file already exists, use the 'a' flag instead. For more info, see here.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments