Home > Back-end >  Recursively updating date format in file path
Recursively updating date format in file path

Time:09-06

I am writing a script that will iterate through a directory with 20 sub directories each with 12 sub directories containing 365 .tar files. Each file has a structure like this: MODIS_20050301.tar,MODIS_20050302.tar,...,MODIS_20050331.tar

The goal is to change the format of each folder to this: MODIS_2005-03-01.tar,MODIS_2005-03-02.tar,...,MODIS_2005-03-31.tar

I have written a test script to try and implement this before I run it on each of the total 6850 files but am running into a FileNotFoundError: [WinError 2] The system cannot find the file specified: 'MODIS_20050527.txt' -> 'MODIS_2005-05-27.txt'

I created a test directory Fake\ which has a two sub directories Fake\Year\Month which contain my two test files MODIS_20050527 and MODIS_20050528 as .txt files.

Below is the script I implemented to do this task but again am running into the FileNotFoundError I am curious if my dir variable is wrong or if there is an issue in my for loop.

dir = r"XXX\Fake"

for root, dirs, files in os.walk(dir):
    for name in files:
        string = 'MODIS_'
        ext = '.txt'
        d = datetime.datetime.strptime(name[7:15],'%Y%m%d').date()
        new_name = string   str(d)   ext
        os.rename(name,new_name)

CodePudding user response:

Implementing a solution from "Python loop through folders and rename files" I believe I found the solution. The issue being the root of the file. An update to my code now looks like this:

for root, dirs, files in os.walk(dir):
    for name in files:
        string = 'SNODAS_'
        #ext = '.txt'
        d = datetime.datetime.strptime(name[7:15],'%Y%m%d').date()
        new_name = string   str(d)
        os.rename(os.path.join(root,name),os.path.join(root,new_name))

I am a bit confused now though as to whether I need to include the ext variable or not I ran it both times with and without it and the resulting name change was the same. IE MODIS_2005-05-27.

CodePudding user response:

name is just the name of each file, but os.rename needs the full path.

You have the remainder of the path in root from os.walk, so you just need to join this to the old and new file names in the last line of the for loop.

Change the last line to:

os.rename(os.path.join(root, name), os.path.join(root, new_name))

Also covered in this answer:

Need the path for particular files using os.walk()

By the way, I had to change [7:15] to [7:14] to get the code to run correctly.

  • Related