Skip to content

Instantly share code, notes, and snippets.

@sid86-dev
Created October 13, 2021 19:23
Show Gist options
  • Save sid86-dev/8a49f844ec00e51eb079ffed4e52f6cc to your computer and use it in GitHub Desktop.
Save sid86-dev/8a49f844ec00e51eb079ffed4e52f6cc to your computer and use it in GitHub Desktop.
Extract names of files which are in a folder to a csv file
import os
import csv
class Readname():
def __init__(self, loc):
self.loc = loc
# get list of filenames in the folder
def listNames(self):
filenames = os.listdir(self.loc)
l = [filename for filename in filenames]
return l
# write the list of filenames into csv file
def collect(self, list):
try:
with open('filenames.csv', 'w', newline='') as f:
thewriter = csv.writer(f)
thewriter.writerow(['FILENAMES'])
for name in list:
thewriter.writerow([name])
except:
return "No data to write"
if __name__=="__main__":
path = r'C:\\sid_programmer\\PYTHON\Python Course'
l = Readname(path)
files = l.listNames()
l.collect(files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment