Feb-10-2019, 04:31 AM
Hi all,
I have googled ad nauseaum, but I can't seem to find a solution for printing contents of a folder to a .csv
File "C:\Users\Randy\Documents\5CRIPT5\ListFiles.py", line 26, in <module>
with Path(csvfilename).open(mode="w+") as p:
NameError: name 'Path' is not defined
Kinda stuck here. It looks as though path should be a variable?
I'm using IDLE 3.7 and on WINDOWS 10, Lenovo Inspiron
Thanks,
I have googled ad nauseaum, but I can't seem to find a solution for printing contents of a folder to a .csv
###A script to list pdf files in a folder ###gisdude 02/09/19 ### import os, sys, glob, csv, pathlib ###First, let's change the dir with os os.chdir("C:\\Users\\Randy\\Documents\\BOOKS") ###Second, let's now get the list of files in this directory files = glob.glob('*.pdf') for file in glob.glob("*.pdf"): print (file) ###This prints to the IDLE console ###But, I want to print to a csv file ####for filename in glob.iglob ('*.pdf'): ###with open('Listofpdf', 'filename') as csvfile: ###writer = csv.writer(csvfile, delimter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) ###writer.writerrow([('{}\t.format(elem))]) searchdir = "C:\\Users\\Randy\\Documents\\BOOKS" csvfilename = "listofpdf.txt" with Path(csvfilename).open(mode="w+") as p: files = Path(searchdir).glob('*.py') p.write(f"{' '.join(str(file) for file in files)}\n")I get an error, "Traceback (most recent call last):
File "C:\Users\Randy\Documents\5CRIPT5\ListFiles.py", line 26, in <module>
with Path(csvfilename).open(mode="w+") as p:
NameError: name 'Path' is not defined
Kinda stuck here. It looks as though path should be a variable?
I'm using IDLE 3.7 and on WINDOWS 10, Lenovo Inspiron
Thanks,
"Human history becomes more and more a race between education and catastrophe." - H. G. Wells (1866-1946)