Friday, March 19, 2010

[Python] file list_converter

#!/usr/bin/env python3.1

#把file list檔案轉成目前的os目錄格式

import sys
import os
import re

svn = 'svn up ' #svn program and argument


pattern = re.compile('(?:\\\\|/)')
pattern2 = re.compile('(?:\\n+|\\r+)') #line feed & carriage return

with open(sys.argv[1], 'r') as f:
    with open(sys.argv[1] + '_out.txt', 'w') as out:
        with open(sys.argv[1] + '_one.txt', 'w') as one:
            one.write(svn)
            for line in f:
                line = pattern.sub(os.sep, line)
                out.write(line)
                line = pattern2.sub(' ', line)
                one.write(line)
        one.close()
    out.close()
f.close()

No comments: