Python Notes
Contents
FILES_FOLDERS,
IO, JSON,
TIME, GUI, Data Structures,
Strings, Dirs & Files,
Admin, os, sys,
Control Flow, Introspection,
Main Templates, Class Templates, Modules,
Parsing
#FILES_FOLDERS - os_walk_filter_dirpaths
# -*- coding: utf-8 -*-
r"""
ref: C:\1d\PythonPjs\pyUtilPj\src\os_walk\listPYfiles2.py
google: python os.walk recursive | Python os walk recursive depth | python os walk
1. [TRAVERSING DIRECTORIES RECURSIVELY](https://www.bogotobogo.com/python/python_traversing_directory_tree_recursively_os_walk.php)
2. [Manipulating Python os.walk Recursion](https://www.ostricher.com/2014/12/manipulating-python-os-walk-recursion/)
3. [python - Do I understand os.walk right?](https://stackoverflow.com/questions/10989005/do-i-understand-os-walk-right)
"""
import sys, os
EXCLUDED_STARTSWITH_DIRPATH_LIST = [r'C:\1d\PythonPjs\pyUtilPj\src\prettyprint_stringlist_n_upPj', r'C:\1d\PythonPjs\pyUtilPj\src\test\in']
TOP_DIR_PATH = r'C:\1d\PythonPjs\pyUtilPj\src'
TOP_DOWN = True
count = 0; stopcount = 500;
for dirpath, dirnames, filenames in os.walk(TOP_DIR_PATH, topdown=TOP_DOWN):
# filter out dirpaths
if any(itm in dirpath for itm in ['.git', 'node_modules']): continue
if any(dirpath.startswith(itm) for itm in EXCLUDED_STARTSWITH_DIRPATH_LIST): continue
print('dirpath: %s\n dirnames: %s\n filenames%s\n\n'%(dirpath, dirnames, filenames))
count += 1;
if count > stopcount: break
#FILES_FOLDERS - os_walk_simple
# -*- coding: utf-8 -*-
r"""
ref: C:\1d\PythonPjs\pyUtilPj\src\os_walk\listPYfiles2.py
google: python os.walk recursive | Python os walk recursive depth | python os walk
1. [TRAVERSING DIRECTORIES RECURSIVELY](https://www.bogotobogo.com/python/python_traversing_directory_tree_recursively_os_walk.php)
2. [Manipulating Python os.walk Recursion](https://www.ostricher.com/2014/12/manipulating-python-os-walk-recursion/)
3. [python - Do I understand os.walk right?](https://stackoverflow.com/questions/10989005/do-i-understand-os-walk-right)
"""
import sys, os
TOP_DIR_PATH = r'C:\1d\PythonPjs\pyUtilPj\src'
TOP_DOWN = True
count = 0; stopcount = 20;
for dirpath, dirnames, filenames in os.walk(TOP_DIR_PATH, topdown=TOP_DOWN):
print('dirpath: %s\n dirnames: %s\n filenames%s\n\n'%(dirpath, dirnames, filenames))
count += 1;
if count > stopcount: break
#GUI - tk_simple_dialog
# -*- coding: utf-8 -*-
r""" tkAskString.py uses TK to ask the user for a string & prints it out
Usage: ./tkAskString.py
Sample: ./tkAskString.py
see:
1. ["Example 2" - Python tkinter.simpledialog.askstring() Examples](https://www.programcreek.com/python/example/100669/tkinter.simpledialog.askstring)
2. [How do I get rid of Python Tkinter root window?](https://stackoverflow.com/questions/1406145/how-do-i-get-rid-of-python-tkinter-root-window)
3. [Tkinter Dialogs (Python3.10)](https://docs.python.org/3/library/dialog.html)
4. google: tkinter simpledialog set width | tkinter simpledialog keyword arguments
5. ["add extra tabs at the end of your prompt"](https://stackoverflow.com/a/69396488/601770)
"""
import tkinter as tk
from tkinter import simpledialog
# root = tk.Tk()
def main():
root = tk.Tk()
root.withdraw()
uname = simpledialog.askstring("My Title", "Enter username:\t\t\t") # Tabs make it wide as i want
print(uname)
return uname
if __name__ == "__main__":
main()
#IO - ReadFileToList1Liner
f = open(r'mytest.txt', 'r', encoding="utf-8"); l = f.read().splitlines(); f.close()
#IO - ReadFileToString1Liner
f = open(r'mytest.txt', 'r', encoding="utf-8"); s = f.read(); f.close()
#JSON - JSONtoPythonObj
import json
f = open(r'mytest.json', 'r', encoding="utf-8"); jsonstr = f.read(); f.close()
my_python_obj = json.loads(jsonstr)
print(my_python_obj)
#JSON - PythonObjToJSON
import json
my_python_obj = {"first_name": "Guido", "last_name":"Rossum"}
my_python_obj_json_str = json.dumps(my_python_obj)
f = open('my_python_obj.json', 'w', encoding="utf-8"); f.write(my_python_obj_json_str); f.close()
#TIME - TimeStampsDuration
import datetime
dt_begin = datetime.datetime.now()
# DO YOUR STUFF IN HERE
dt_end = datetime.datetime.now()
print( "End Timestamp: %s"%(str(dt_end).replace(':', '.').replace(' ', '_')) )
print( "Begin Timestamp: %s"%(str(dt_begin).replace(':', '.').replace(' ', '_')) )
print( " (End - Begin): %s"%(dt_end - dt_begin))