Last active
January 1, 2016 16:49
-
-
Save martin-ueding/8173440 to your computer and use it in GitHub Desktop.
Rename lecture notes files for physik511.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
# Copyright © 2013-2014 Martin Ueding <[email protected]> | |
# Licensed under The GNU Public License Version 2 (or later) | |
import argparse | |
import re | |
import shutil | |
__docformat__ = "restructuredtext en" | |
pattern = re.compile(r'Vlsg_?(\d{1,2})(\w{3})(\d{4})\.pdf') | |
month_names = { | |
'Okt': 10, | |
'Ok1': 10, | |
'Nov': 11, | |
'N0v': 11, | |
'Dez': 12, | |
'Jan': 1, | |
'Feb': 2, | |
} | |
def main(): | |
options = _parse_args() | |
for filename in options.infile: | |
rename_file(filename) | |
def rename_file(filename): | |
print(filename) | |
m = pattern.match(filename) | |
if m: | |
year = int(m.group(3)) | |
month_word = m.group(2) | |
day = int(m.group(1)) | |
month = month_names[month_word] | |
new_name = 'Vorlesung_{:04d}-{:02d}-{:02d}.pdf'.format(year, month, day) | |
shutil.move(filename, new_name) | |
def _parse_args(): | |
""" | |
Parses the command line arguments. | |
:return: Namespace with arguments. | |
:rtype: Namespace | |
""" | |
parser = argparse.ArgumentParser(description="") | |
parser.add_argument("infile", type=str, nargs="+", help="Files with original name") | |
return parser.parse_args() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment