# Chapter 6 # Datetime Module page 67 ''' String Formats for Datetime in Python %a Weekday, short format Mon Tue ... %A Weekday Monday Tuesday ... %w Weekday 0 is Sunday ... %d Day of Monday 2 digit format 01-31 %b Month name, short version Jan, Feb, ... %B Month name, January .... %m Month as number 2 digit format January 01, ... %y Year short format 2 digit 2024 would be 24 %Y Year 2024 .... ''' import datetime currentDateTimeStamp = datetime.datetime.now() print(currentDateTimeStamp) # formats the WHOLE string to template print(currentDateTimeStamp.strftime("%Y")) # f'2024' # value itself of a part of date or time the_year = currentDateTimeStamp.year #method name without the () print(the_year) # Module.Class.Method().year() directlyaccess_year = datetime.datetime.now().year # more resource print(directlyaccess_year) # Collection Col1, Col2, Col3 mystamp = datetime.datetime.now() mymonth = mystamp.month myday = mystamp.day myyear = mystamp.year # dd/yyyy/mm print(myday) print(mymonth) print(myyear) storeddata = f'{mymonth}/{myday}/{myyear}' print(storeddata) print( type(storeddata) ) # string NOT datetime type() # Collect the data from a source and reformat converteddata = datetime.datetime(myyear, mymonth, myday) print( converteddata ) print( type(converteddata) ) # datetime type() # OS Module page 69 os and interpreter layer import os # Get the current working directory cwd = os.getcwd() print(cwd) # Create a new subdir and then change working directory # Create a new dir newDir = "cart" if not os.path.exists(newDir): os.mkdir(newDir) else: print(f'{newDir} folder already exists') # Change the working dir to the new directory # store the current directory currentProgramPath = os.getcwd() print(currentProgramPath) # change to the cart # use join() NOT string + to create path pathToCart = os.path.join(currentProgramPath, newDir) # C:\Users\Student\Documents\PythonLevel2\cart print(pathToCart) os.chdir(pathToCart) # change dir to cart print("New working dir: ", os.getcwd()) # cart # when done with the cart, change back to program print("When done with cart, go BACK to program") os.chdir(currentProgramPath) print("Original working dir: ", os.getcwd()) # List all files and folders in a directory path object_list = os.listdir(currentProgramPath) # List [] print(object_list) # [' ',' ', ....] for o in object_list: print(o) # Logger Factory Page 73 # import the Standard Module import logging # Create a logger factory logger = logging.getLogger("logger_name") logger.debug("Debug Logging") logger.info("Information Logging") logger.warning("Logging at warning") logger.error("Logging at error") # Define a variable that identifies the sys system = "system1" for l in range(10): logger.warning("%d messages reported in %s", l, system) #logger.warning("{} messages reported in {}", l, system)