print console out to a txt or csv file

Peter Otten __peter__ at web.de
Sun Feb 10 11:02:40 EST 2019


GISDude wrote:

> Hi all,
> I have been working on some code to list the files of a folder that has
> .pdf extension. I have the basic code to create a list, but it prints that
> list to the console. I'd like my code to write a txt file that contains
> that list (to later import into excel).
> 
> ###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")

I have come to the conclusion that keeping track of the full path is much 
easier than keeping track of what directory you are in at the moment.
I recommend that you never use os.chdir().

> ###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))])
>     from pathlib import Path
>     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")
> 
> At this point, the pathlib mod seems like it should create the file?
> 
> I'm on WINDOWS 10 and IDLE 3.7.
> 
> Thanks for any help,
> R`

This looks rather messy, mainly because you are keeping every attempt you 
made. I recommend that you remove the code you don't use or that doesn't 
work as soon as possible.

Here are two ways to write the list of filenames to a csv file:

(1) Using traditional file path manipulation routines:

PDFFOLDER = "C:\\Users\\Randy\\Documents\\BOOKS"
CSVFILE = "files.txt"

filenames = glob.glob(os.path.join(PDFFOLDER, "*.pdf"))

with open(CSVFILE, "w", newline="") as outstream:
    writer = csv.writer(outstream)
    writer.writerows([path] for path in filenames)

Note that you are actually writing a list of lists; omitting the inner list 
leads to writing one column per character in the filename.


(2) Using pathlib's Path objects:

PDFFOLDER = pathlib.Path("C:\\Users\\Randy\\Documents\\BOOKS")
CSVFILENAME = pathlib.Path("files.txt")

filenames = PDFFOLDER.glob("*.pdf")
with CSVFILENAME.open("w", newline="") as outstream:
    writer = csv.writer(outstream)
    writer.writerows([path] for path in filenames)


If you want the filename without directory replace path in the single item 
lists with os.path.basename(path) or path.name respectively.




More information about the Python-list mailing list