How I created a log file to track stuff

How I created a log file to track stuff


After a bit of experimentation?using win32com.client and a couple of other modules I figured out how to make a log file.


What it does is very simple, every time an instance of?SolidWorks is opened using the script it will also open and log the user name and a time stamp in the next available line in a .txt file.


It's simple but the concept can be reused for many different applications. Another immediate use case I thought of would be to create an error log to capture execution errors inside of try....except.... blocks in a code project. The catch blocks could write text to the .txt file explaining the error in detail.


So basically what I did was import the win32com.client module, I also imported datetime and getpass.


I also created a function that opens the .txt file, from a file path that I specified. The function then appends the text to the end of the file, and closes it. This function is invoked towards the end of the script.


I then used win32com.client to open SolidWorks - but any application can be opened using this script, just change the text.


Used datetime to get a timestamp of the current system date and time down to the millisecond.


I then used getpass to get the name of the system user that is currently logged into windows.


From here I concatenated the system username, the current date, and the current time. The date and time had to be converted from a date object into strings for this to work.


I also stored the file location path to the .txt file in a string?to pass into the function I had created earlier.


I then call the function and pass in the file path and the concatenated text that is to be written into the log.


win32com.client is then called upon once again to close SolidWorks.


The only downside to all of of this is that in order for an entry to be logged in the document, SolidWorks must be opened through running the script, the last line of code would also have to be commented out to allow the user to continue using their session.


import win32com.client
from datetime import datetime
import getpass

def enter_text_into_file(file_path, text):
  # Create an instance of the FileSystemObject
  fso = win32com.client.Dispatch("Scripting.FileSystemObject")
  # Create a new text file or open an existing one
  # 2 = ForWriting, # 8 for appending True = Create if not exists
  file = fso.OpenTextFile(file_path, 8, True) 
  # Write the text into the file
  file.Write(text)
  # New line
  file.Write("\n")
  # Close the file
  file.Close()

# Create an instance of the SolidWorks applicatio
swApp = win32com.client.Dispatch("SldWorks.Application")

# Get the current date and time
current_datetime = datetime.now()

# Extract the date and time components
current_date = current_datetime.date()
current_time = current_datetime.time()

# Get the system user
system_user = getpass.getuser()

# system user, date and time passed into the function
date_and_time = system_user+ ": " + str(current_date) + "-" + str(current_time)

# Example usage
file_path = "C:/Users/georg/Desktop/Python Projects/Win32/open_log.txt"
enter_text_into_file(file_path, date_and_time )
swApp.Visible = False

# Close SolidWorks
swApp.ExitApp()        

#python #work #windows #pythonprogramminglanguage #project #programming #coding #solidworks #solidworksdesign #pmp #developer #raspberrypi














要查看或添加评论,请登录

George Check的更多文章

社区洞察

其他会员也浏览了