標籤:file mod alike mat main ++ c++ style sys.argv
1 import sys 2 import re 3 4 # argv 是個列表,內容是檔案的路徑 5 def erase_mark(args): 6 mark = re.sub("//*.{1,1000}/*/","",args) 7 mark = re.sub("//.{1,1000}\n","",mark) 8 mark = re.sub("#.{1,1000}\n","",mark) 9 return mark10 11 def erase_space(args):12 space = args.replace(" ","")13 space = space.replace("\n","")14 return space15 16 def show_different(args_one,args_two):17 info = "[decument one: %s decument two: %s]" % (args_one,args_two)18 print(info)19 20 21 def display(information,condition):22 if condition:23 print(information[0])24 else:25 print(information[1])26 27 28 def compare_same(args_one,args_two):29 cycle = args_one if len(args_one) < len(args_two) else args_two30 for i in range(len(cycle)):31 if args_one[i] != args_two[i]:32 show_different(args_one[i],args_two[i])33 display(["decument is alike","decument is different"], args_one == args_two)34 35 36 def achieve(args):37 cover = open(args["decument_one"], mode = "r", encoding = "utf-8")38 primary = open(args["decument_two"], mode = "r", encoding = "utf-8")39 cover_info, primary_info = cover.read(), primary.read()40 cover_info, primary_info = erase_mark(cover_info), erase_mark(primary_info)41 cover_info, primary_info = erase_space(cover_info), erase_space(primary_info)42 compare_same(cover_info, primary_info)43 44 45 def main():46 if len(sys.argv) < 3:47 first_file,second_file = input("decoment one path:").strip(), input("decoment two path:").strip()48 else:49 first_file,second_file = sys.argv[1],sys.argv[2];50 args = {"decument_one":first_file,"decument_two":second_file};51 achieve(args)52 53 54 if __name__ == "__main__":55 main()
Python寫的C/C++代碼比較