ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • <PYTHON>[지정한 경로 안의 모든 키워드를 변경]
    Flower in my dev/Python 2017. 11. 16. 09:18

    [지정한 경로 안의 모든 키워드를 변경]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    # -*- coding : utf-8 -*-
     
    import os
    import sys
    from subprocess import check_output
     
     
    class CheckString():
        def __init__(self, path, str01, str02):
            self.path = path
            self.before_str = str01
            self.after_str  = str02
     
        def check_string(self):
            file_list = self.get_filepath()
            for file in file_list:
                self.file_controller(file)
     
        def get_filepath(self):
            path_rs = check_output("grep -r '%s' %s" % (self.before_str, self.path), shell=True).decode()
            tmp_list = path_rs.split('\n')[:-1]
            path_list = [tmp.split(':',1)[0for tmp in tmp_list if 'Binary file' not in tmp]
            return path_list
     
        def file_controller(self, fp):
            f = open(fp, 'r')
            temp_str = f.read()
            f.close()
            print(temp_str)
            rs_str = temp_str.replace(self.before_str, self.after_str)
            print(rs_str)
            f = open(fp, 'w')
            f.write(rs_str)
            f.close()
     
        def run(self):
            self.check_string()
     
    if __name__ == '__main__':
        CS = CheckString(sys.argv[1], sys.argv[2], sys.argv[3])
        CS.run()
    cs

    댓글

Designed by Tistory.