0

I'm trying to write a python script to convert a folder of .asc files (365 files for every year in different folders organized by year) that have the yearmonthdate in their filename to have the yearjuliandate instead and the julian date needs to be 3 digits (ie 1 = 001). The format they are in: ETos19810101.asc. I want them to be as: ETos1981001.asc

How do I write this in Python where I can iterate over each file and convert it to the correct julian day?

I'm trying to write a Python script to convert a folder of .asc files (365 files for every year in different folders organized by year) that have the yearmonthdate in their filename to have the yearjuliandate instead and the julian date needs to be 3 digits (ie 1 = 001).

  • The format they are in: ETos19810101.asc
  • I want them to be as: ETos1981001.asc

How do I write this in Python where I can iterate over each file and convert it to the correct julian day?

I have this so far:

import os.path, os, glob

for filename in glob.glob(filepath + "/*.asc"):
    jdate = '%03d' %doy #creates 3 digit julian date
    doy = doy + 1
    filename.replace(int[-8:-4], jdate + 1)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
MattS
  • 3
  • 2

3 Answers3

1

Given a file name as following (you can iterate your file system with os.walk)

filename = 'ETos19810101.asc'

First of all you have to split the filename to get every significant parts:

import os
name, ext = os.path.splitext(filename)
prefix = name[0:-6] # negative prefix => string end as reference
strdate = name[-6:]

Then you can parse the date:

from datetime import datetime
date = datetime.strptime(strdate, '%Y%m%d')

Now you are able to join everything together (%Y%j format the date the way you want):

newfilename = '{prefix}{date:%Y%j}{ext}'.format(prefix=prefix, date=date, ext=ext)

Finally rename the file:

os.rename(filename, newfilename)

Note that the last instruction will fail if newfilename already exists. To fix this issue you have to remove the file, if it exists:

if os.path.exists(newfilename):
    os.remove(newfilename)
os.rename(filename, newfilename)
alain
  • 657
  • 6
  • 8
0

For working with dates you should use the datetime module. Parse the date string with strptime. There's no function to return a julian date, but it's easy to create one:

def julian_day(dt):
    jan1 = dt.replace(month=1, day=1)
    return 1 + (dt - jan1).days
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

Use the '%j specifier along with datetime.strptime and os.rename and the various os.path commands:

from datetime import datetime
from glob import glob
import os

for filename in glob(os.path.join(filepath, 'ETos*.asc')):
    try:
        dt = datetime.strptime(os.path.basename(filename), 'ETos%Y%m%d.asc')
    except ValueError as e:
        continue # rest of file name didn't have valid date - do the needful
    os.rename(filename, os.path.join(filepath, format(dt, 'ETos%Y%j.asc')))

You'll probably want a bit of handling around that and adjust to take into account your path, but that's the general principle.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • Nice! Thank you, that worked! Don't completely understand how it worked yet (i'm a Python newb) but it worked! – MattS May 18 '15 at 15:57
  • @MattS no worries have a read through the docs for `os.path.basename` and `datetime.strptime` and `os.path.join` - all should become more clear than I can summarise without just copy pasting in docs :) – Jon Clements May 18 '15 at 15:58