Unanswered
Write me a program that would automatically change all of these file names to look like the examples I gave
Prompt:
I have hundreds of files with a date naming pattern. Here are 5 example files: 1. "20220914230334" 2. "20220915212429" 3. "20220916113012" 4. "20220917113220" 5. "20220918113356". The first four numbers in each of the names represent the year, the next two represent the month, the two after that represent the day, and the other 6 are unneeded. For example, file 1 that I mentioned would translate to "September 14th, 2022", and file 2 would translate to "September 15th, 2022". Write me a program that would automatically change all of these file names to look like the examples I gave.
import os
# the path to the directory where the files are located
path = '/path/to/files/'
# get a list of all the files in the directory
files = os.listdir(path)
# loop through each file
for file in files:
# split the file name and extension
name, extension = os.path.splitext(file)
# get the year, month, and day from the file name
year = name[0:4]
month = name[4:6]
day = name[6:8]
# create the new file name
new_name = f"{year}-{month}-{day}{extension}"
# rename the file
os.rename(f"{path}/{file}", f"{path}/{new_name}")
print("Done!")
1K Views
0
Answers
one year ago
one year ago
Tags