- A regular expression is a set of string that satisfies certain format.
- regular expression plus linux command can make life easy.
- browse books by authors' name.
- browse books by category.
#!/usr/bin/python
# Filename: SortLibrary.py
# 2013/03/06
# Version: 1.0
"""
A script to automatically sort the books by authors' name.
Usage: SortLibrary.py sorting_directory_here
"""
import sys
import os
import re
def sortLib(dir):
'''
sort the books by author name, use python regular expression
'''
filenames = os.listdir(dir)
for filename in filenames:
match = re.search(r' - (.+)\.mobi', filename)
if match:
path = os.path.join(dir,match.group(1).replace(' ','_'))
if not os.path.exists(path):
mkdir_command = 'mkdir '+ path
os.system(mkdir_command)
mv_command = 'mv {0}/"{1}" {2}'.format(dir,filename,path)
os.system(mv_command)
def main():
sortLib(sys.argv[1])
if __name__ == '__main__':
main()
outcome:
No comments:
Post a Comment