Pathlib’s .glob and Pretty Print

Glob and Pretty Print – a demo too good to waste

I had already “dis-included” Pretty Print from the new expanded version of TB1 when I began vetting the syntax for using glob from pathlib. This is a short and simple demo using .glob that should convince anybody that there is a time and place to know and use Pretty Print.

This is the code that I used on my computer but of course this will not work for you since you have different stuff in different places.  To make it work find the address of a folder with lots of py test files and plug it in where I have the my_path = below.

from pathlib import Path
import pprint
#demo works under windows
my_path = "D:\Py Pi Code and Docs"  #change this line to your local folder
print("I am looking for py files in a folder.")
print("This is generic print - yuk, messy!\n")
print(sorted(Path(my_path).glob('*.py'))) #note syntax for Path and .glob
print("\nThis is using Pretty Print\n")
pp = pprint.PrettyPrinter(depth=6)  # create an pp definition
pp.pprint(sorted(Path(my_path).glob('*.py')))
print("\n'nuff said")

So in my case, here is the result that convinced me to go back and add Pretty Print to the new version of TB1.

I am looking for py files in a folder.
This is generic print – yuk, messy!

[WindowsPath(‘D:/Py Pi Code and Docs/capture environment.py’), WindowsPath(‘D:/Py Pi Code and Docs/Demo_Sudoku Mike Barnet.py’), WindowsPath(‘D:/Py Pi Code and Docs/get and display environment.py’), WindowsPath(‘D:/Py Pi Code and Docs/glob and prettyprint.py’), WindowsPath(‘D:/Py Pi Code and Docs/HEADER SETUP 1.py’), WindowsPath(‘D:/Py Pi Code and Docs/HEADER SETUP 2 1024X768.py’), WindowsPath(‘D:/Py Pi Code and Docs/openGoogle.py’), WindowsPath(‘D:/Py Pi Code and Docs/prettyprint.py’), WindowsPath(‘D:/Py Pi Code and Docs/PySimpleGUI amortization 060620.py’), WindowsPath(‘D:/Py Pi Code and Docs/temp.py’), WindowsPath(‘D:/Py Pi Code and Docs/temptest 3.py’), WindowsPath(‘D:/Py Pi Code and Docs/temptest.py’), WindowsPath(‘D:/Py Pi Code and Docs/temptest2.py’), WindowsPath(‘D:/Py Pi Code and Docs/temptest4.py’), WindowsPath(‘D:/Py Pi Code and Docs/temptest5.py’), WindowsPath(‘D:/Py Pi Code and Docs/temptest6.py’), WindowsPath(‘D:/Py Pi Code and Docs/temptest7.py’), WindowsPath(‘D:/Py Pi Code and Docs/temptest8.py’)]

This is using Pretty Print

[WindowsPath(‘D:/Py Pi Code and Docs/capture environment.py’),
WindowsPath(‘D:/Py Pi Code and Docs/Demo_Sudoku Mike Barnet.py’),
WindowsPath(‘D:/Py Pi Code and Docs/get and display environment.py’),
WindowsPath(‘D:/Py Pi Code and Docs/glob and prettyprint.py’),
WindowsPath(‘D:/Py Pi Code and Docs/HEADER SETUP 1.py’),
WindowsPath(‘D:/Py Pi Code and Docs/HEADER SETUP 2 1024X768.py’),
WindowsPath(‘D:/Py Pi Code and Docs/openGoogle.py’),
WindowsPath(‘D:/Py Pi Code and Docs/prettyprint.py’),
WindowsPath(‘D:/Py Pi Code and Docs/PySimpleGUI amortization 060620.py’),
WindowsPath(‘D:/Py Pi Code and Docs/temp.py’),
WindowsPath(‘D:/Py Pi Code and Docs/temptest 3.py’),
WindowsPath(‘D:/Py Pi Code and Docs/temptest.py’),
WindowsPath(‘D:/Py Pi Code and Docs/temptest2.py’),
WindowsPath(‘D:/Py Pi Code and Docs/temptest4.py’),
WindowsPath(‘D:/Py Pi Code and Docs/temptest5.py’),
WindowsPath(‘D:/Py Pi Code and Docs/temptest6.py’),
WindowsPath(‘D:/Py Pi Code and Docs/temptest7.py’),
WindowsPath(‘D:/Py Pi Code and Docs/temptest8.py’)]

’nuff said