defremove_comments_from_file(file_path): with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines()
with open(file_path, 'w', encoding='utf-8') as file: for line_number, line in enumerate(lines, start=1): # 如果当前行包含http,则不移除注释 if'http'in line: file.write(line) else: # 移除行尾的注释 line = re.sub(r'//.*', '', line) # 移除行内的注释 line = re.sub(r'/\*.*?\*/', '', line) file.write(line) # 输出已处理的行数 print(f'Removed comments from line {line_number} in file {file_path}')
defremove_comments_from_folder(folder_path): for root, dirs, files in os.walk(folder_path): for file in files: if file.endswith('.ts') or file.endswith('.tsx'): # 修改条件,同时处理.ts和.tsx文件,js项目则为 js、jsx file_path = os.path.join(root, file) remove_comments_from_file(file_path)
# 这里的文件夹路径更改为你要移除注释工程的具体路径 folder_path = '/path/project_folder' remove_comments_from_folder(folder_path) print('Comment removal process completed.')