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)