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
#2
(Feb-10-2019, 04:31 AM)deep_logic Wrote: NameError: name 'Path' is not defined

You didn't import Path:

from pathlib import Path
Reply
#3
(Feb-10-2019, 04:38 AM)scidam Wrote:
(Feb-10-2019, 04:31 AM)deep_logic Wrote: NameError: name 'Path' is not defined

You didn't import Path:

from pathlib import Path

That did work, however, It's not creating the .csv file.

Still grindin'...
"Human history becomes more and more a race between education and catastrophe." - H. G. Wells (1866-1946)
Reply
#4
Just a follow up. It seems that the network at my work has been locked down. So, I'm not able to script codes in python and create files and such. I can do SOME things, but I'm the little fish in the proverbial big pond.

FWIW, I did use this code to create a .txt list and just use that to populate my excel file.

Many thanks for the help and I'm grateful for the pythonista community.
"Human history becomes more and more a race between education and catastrophe." - H. G. Wells (1866-1946)
Reply


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,033 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,278 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,236 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Want to Save Print output in csv file Rasedul 5 10,687 Jan-11-2022, 07:04 PM
Last Post: snippsat
Photo print output none 3lnyn0 4 1,756 Nov-01-2021, 08:46 PM
Last Post: 3lnyn0
  output correction using print() function afefDXCTN 3 10,961 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 1,881 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,045 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Print output not working xninhox 7 3,942 Jan-16-2021, 09:42 AM
Last Post: xninhox
  Output with none, print(x) in function Vidar567 3 2,451 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