# 处理文件夹 deftrim_dir(path): print("dir:" + path) for root, dirs, files in os.walk(path): print("***") print(files) for name in files: trim_file(os.path.join(root,name))
for line in fp_src.readlines(): print("--> 当前是第%d行" %cnt) print(line) cnt += 1 state = S_INIT
if cnt < 7: fp_dst.write(line) continue
for c in line: # State INIT if state == S_INIT: if c == '/': state = S_SLASH elif c == '"': state = S_STR fp_dst.write(c) else: fp_dst.write(c) # State slash: elif state == S_SLASH: if c == '*': state = S_BLOCK_COMMENT elif c == '/': state = S_LINE_COMMENT else: fp_dst.write('/') fp_dst.write(c) state = S_INIT # State Block_comment start / elif state == S_BLOCK_COMMENT: if c == '*': state = S_BLOCK_COMMENT_DOT # State Blcok_comment state * elif state == S_BLOCK_COMMENT_DOT: if c == '/': state = S_INIT elif c == '*': state = S_BLOCK_COMMENT_DOT #再次碰到*还得持续DOT状态,不然会出错 else: state = S_BLOCK_COMMENT
# state line comment elif state == S_LINE_COMMENT: if c == '\n': state = S_INIT fp_dst.write(c) # state str elif state == S_STR: if c =='\\': state = S_STR_ESCAPE elif c == '"': state = S_INIT fp_dst.write(c) # state str_escape elif state == S_STR_ESCAPE: state = S_STR fp_dst.write(c)