Change File Name

Posted on
Fri Jan 14, 2022 8:51 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Change File Name

So I started logging some Indigo activity into txt files, and all works great. The file name that the data is written to is called temp.txt, so what I'd like to do is at 11:59 pm on the last day of the month, change the file name to 2022-01.text, so Indigo will create a new text file with a new log entry basically on the 1st of each month. Obviously this will be a Scheduled event.

I did a bit of research and came up with the basics for changing the file name and that worked great, but need to insert the current year and date into the file name.

I'm assuming the time variable will come out to 2022-01(or something similar), and I'm also guessing I need to add the .txt suffix in some how to complete the new file name, but I'm guessing my elementary NewName variable isn't even close much less isn't even inserted into the new file variable. I've already got a bunch of year/month/day/time variables if that is easier to implement.

Any suggestions would be greatly appreciated. Thanks

Code: Select all
import os
import time
time = (indigo.server.getTime().strftime('%Y-%M'))
NewName = "time"".txt"

old_file = os.path.join("/Users/Kitchen/Desktop", "temp.txt")
new_file = os.path.join("/Users/Kitchen/Desktop", "temp2.txt")
os.rename(old_file, new_file)

Posted on
Fri Jan 14, 2022 11:32 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Change File Name

So I did some more research, and cobbled together this, which works for me. (edited to actually add in the "Songs" into the file name)
Code: Select all
import time
time = (indigo.server.getTime().strftime('%Y-%m')) #2022-01

current_date_and_time_string = str(time)
type = " Songs"
extension = ".txt"

new_name =  current_date_and_time_string + month + type + extension #new filename is 2022-01 Songs.txt

import os
old_file = os.path.join("/Users/Kitchen/Desktop", "temp.txt")
new_file = os.path.join("/Users/Kitchen/Desktop", new_name)
os.rename(old_file, new_file)

Posted on
Sat Jan 15, 2022 1:38 pm
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Change File Name

For a more pythonic approach, check out the TimedRotatingFileHandler class for use with the logging class. This will do rollovers whenever you want (including at midnight) and will maintain a fixed number of backups. Then you create your own logger instance and attach an instance of that handler to it:

Code: Select all
import logging
# Create an instance of the file handler
my_handler = logging.handlers.TimedRotatingFileHandler("my_log", when="midnight", backupCount=30)
# Make sure your handler will handle any log level
my_handler.setLevel(logging.DEBUG)
# Get a logger
my_logger = logging.getLogger('my_script'
# Attach your handler to it
my_logger.addHandler(my_handler)
# Done - that's all you need to do to configure it

# Use it with any of the standard log types
my_logger.debug('debug message')
my_logger.info('info message')
my_logger.warning('warning message')
my_logger.error('error message')
my_logger.critical('critical message')
# You can set your logger to any log level using my_logger.setLevel(logging.Info) and it will only log messages at that level or higher (in this case, no debug)


The snippit above will configure your handler to rotate at midnight, keep 30 days of backups, write out all messages at any level. It also creates the logger for your script (defaults to INFO I believe) that you can use to log messages. You can change the log level that will be sent to the handler by setting the level as described in the comments.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Jan 15, 2022 6:04 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Change File Name

Thanks Jay

I'll take a deep dive and see how I can make use of this.

Posted on
Tue Feb 01, 2022 2:46 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Change File Name

So for the record, I did get this to work. Since this is the first change of month since I worked on this, I'm happy to say the python script I used did exactly what I asked it to do. At 11:59pm, it changed the file names to exactly what I wanted them to be.

Thanks for everyone's help and suggestions! :D

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests