I’m trying to solve a problem at work, I am not a developer but work in general IT operations and am trying to learn a bit here and there, so I may be way off right now with what I’m trying to do here. I’ve just been utilizing online resources, here and a little bit from the book Automate the Boring Stuff with Python. Here is my objective:
I have two files that are automatically placed in a folder on my computer every morning at the same time using a post processor, and I need to add yesterday’s date to the end of the file names before I upload them to an FTP server which I have do each morning around the same time. I am trying to write a Python script that I can somehow schedule to run each morning right after the files are placed in the folder, which will append yesterday’s date in MMDDYYYY format. For example, if the files are called “holdings.CSV” and “transactions.CSV” when they are placed in the folder, I need to rename them to “holdings01112022.CSV” and “transactions01112022.CSV”. I only want to rename the new files in the folder, the files from previous days with the dates already appended will remain in the folder. Again, I’m a total beginner, so my code may not make sense and there may be superfluous or redundant lines, I’d love corrections… Am I going down the right path here, am I off altogether? Any suggestions?
import os, re from datetime import date from datetime import timedelta directory = 'C:Usersmemain foldersubfolder' filePattern = re.compile('%s.CSV', re.VERBOSE) for originalName in os.listdir('.'): mo = filePattern.search(originalName) if mo == None: continue today = date.today() yesterday = today - timedelta(days = 1), '%M%D%Y' for originalName in directory: newName = originalName + yesterday os.rename(os.path.join(directory, originalName), os.path.join(directory, newName))
Any help is appreciated. Thanks.
Advertisement
Answer
Here’s a short example on how to code your algorithm.
import pathlib from datetime import date, timedelta if __name__ == '__main__': directory = pathlib.Path('/Users/cesarv/Downloads/tmp') yesterday = date.today() - timedelta(days=1) for file in directory.glob('*[!0123456789].csv'): new_path = file.with_stem(file.stem + yesterday.strftime('%m-%d-%Y')) if not new_path.exists(): print(f'Renaming {file.name} to {new_path.name}') file.rename(new_path) else: print(f'File {new_path.name} already exists.')
By using pathlib
, you’ll simplify the handling of filenames and paths and, if you run this program on Linux or macOS, it will work just the same.
Note that:
- We’re limiting the list of files to process with
glob()
, where the pattern*[!0123456789].csv
means “all filenames that do not end with a digit before the suffix (the[!0123456789]
represents one character, before the suffix, that should not – ! – equal any of the characters in the brackets).” This allows you to process only files that do not contain a date in their names. - The elements given by the
for
cycle, referenced with thefile
variable, are objects of classPath
, which give us methods and properties we can work with, such aswith_stem()
andstem
. - We create a new
Path
object,new_path
, which will have its stem (the file’s name part without the suffix.csv
) renamed to the original file’s name (file.name
) plus yesterday in the format you require by using the methodwith_stem()
. - Since this new
Path
object contains the same path of the original file, we can use it to rename the original file. - As you can see, we can also check that a file with the new name does not exist before any renaming by using the
exists()
method.
You can also review the documentation on pathlib.
If you have any doubts, ask away!