Python - 根据朱利安日逐步重命名文件

时间:2016-10-14 18:49:27

标签: python glob

问题: 我有一堆从组织下载的文件。在他们的数据目录中途,组织改变了命名约定(原因未知)。我希望创建一个脚本,将文件放在目录中,并以相同的方式重命名文件,但只是"回去一天"。

以下是一个文件名称的示例:org2015365_res_version.asc

我需要的是仅将年份(2015365)更改为2015364的逻辑。此逻辑需要跨越几年,因此2015001将为2014365

我想我不确定这是否可行,因为它不适用于当前日期,因此使用像datetime这样的模块似乎不适用。

我提出了部分逻辑。我知道它最多只是基本的,但是想要刺它。

# open all files
all_data = glob.glob('/somedir/org*.asc')

# empty array to be appended to
day = []
year = []

# loop through all files
for f in all_data:
    # get first part of string, renders org2015365
    f_split = f.split('_')[0]
    # get only year day - renders 2015365
    year_day = f_split.replace(f_split[:3], '')
    # get only day - renders 365
    days = year_day.replace(year_day[0:4], '')
    # get only year - renders 2015
    day.append(days)
    years = year_day.replace(year_day[4:], '')
    year.append(years)
    # convert to int for easier processing 
    day = [int(i) for i in day]
    year = [int(i) for i in year]

    if day == 001 & year == 2016:
        day = 365
        year = 2015
    elif day == 001 & year == 2015:
        day = 365
        year = 2014
    else:
        day = day - 1

除了上面的逻辑,我还从this帖子中看到了下面的函数,我不确定将它与上面的部分逻辑结合起来的最佳方法是什么。想法?

import glob
import os


def rename(dir, pattern, titlePattern):
    for pathAndFilename in glob.iglob(os.path.join(dir, pattern)):
        title, ext = os.path.splitext(os.path.basename(pathAndFilename))
        os.rename(pathAndFilename,
                  os.path.join(dir, titlePattern % title + ext))

rename(r'c:\temp\xx', r'*.doc', r'new(%s)')

帮帮我,stackoverflow。你是我唯一的希望。

2 个答案:

答案 0 :(得分:1)

您可以使用datetime模块:

#First argument - string like 2015365, second argument - format
dt = datetime.datetime.strptime(year_day,'%Y%j')
#Time shift
dt = dt + datetime.timedelta(days=-1)
#Year with shift
nyear = dt.year
#Day in year with shift
nday = dt.timetuple().tm_yday

答案 1 :(得分:0)

根据社区的反馈,我能够获得修复从组织下载的文件所需的逻辑!逻辑是最大的障碍。事实证明可以使用datetime模块,我需要阅读更多内容。

我使用os模块将逻辑与批量重命名相结合,我将下面的代码用于帮助可能有类似问题的未来用户!

# open all files
all_data = glob.glob('/some_dir/org*.asc')

# loop through
for f in all_data:
    # get first part of string, renders org2015365
    f_split = f.split('_')[1]
    # get only year day - renders 2015365
    year_day = f_split.replace(f_split[:10], '')
    # first argument - string 2015365, second argument - format the string to datetime
    dt = datetime.datetime.strptime(year_day, '%Y%j')
    # create a threshold where version changes its naming convention
    # only rename files greater than threshold
    threshold = '2014336'
    th = datetime.datetime.strptime(threshold, '%Y%j')
    if dt > th:
        # Time shift - go back one day
        dt = dt + datetime.timedelta(days=-1)
        # Year with shift
        nyear = dt.year
        # Day in year with shift
        nday = dt.timetuple().tm_yday
        # rename files correctly
        f_output = 'org' + str(nyear) + str(nday).zfill(3) + '_res_version.asc'
        os.rename(f, '/some_dir/' + f_output)
    else:
        pass