Created
October 13, 2021 19:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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