Python Forum
print console output to a csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print console output to a csv
#1
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
###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)
Reply


Messages In This Thread
print console output to a csv - by deep_logic - Feb-10-2019, 04:31 AM
RE: print console output to a csv - by scidam - Feb-10-2019, 04:38 AM
RE: print console output to a csv - by deep_logic - Feb-10-2019, 04:42 AM
RE: print console output to a csv - by deep_logic - Feb-21-2019, 06:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,107 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to output one value per request of the CSV and print it in another func? Student44 3 1,351 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,321 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Want to Save Print output in csv file Rasedul 5 10,994 Jan-11-2022, 07:04 PM
Last Post: snippsat
Photo print output none 3lnyn0 4 1,847 Nov-01-2021, 08:46 PM
Last Post: 3lnyn0
  output correction using print() function afefDXCTN 3 11,111 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 1,931 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,139 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Print output not working xninhox 7 4,073 Jan-16-2021, 09:42 AM
Last Post: xninhox
  Output with none, print(x) in function Vidar567 3 2,524 Nov-24-2020, 05:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020