Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều
Nhập khẩu argparse | |
Nhập khẩu os | |
Ngăn chặn re | |
Ví dụ: help = "" " = """For example, to insert content of copyright.txt into .h and .m files under src/ and headers/ dir, | |
Bạn có thể chạy: Python Copyright_header_tool.py Bản quyền.txt ./src ./headers - -pattern = '.*\. [H, m] $' "" " | |
Parser = argparse.argumentparser [description = "công cụ tiêu đề tệp.", = argparse.ArgumentParser[description="File header tools.", | |
Epilog = trợ giúp]=help] | |
Parser.add_argument ["tiêu đề", nargs = 1, type = argparse.filetype ["rb"], help = "Lưu tiêu đề tệp dự kiến của bạn tại đây."].add_argument["header", nargs=1, type=argparse.FileType["rb"], help="save your expected file header here."] | |
Parser.add_argument ["đường dẫn", nargs = "+",.add_argument["paths", nargs="+", | |
Trợ giúp = "Các tệp bên trong các Dirs này sẽ được xử lý."]="files inside these dirs will be processed."] | |
Parser.add_argument ["-mẫu", bắt buộc = true,.add_argument["--pattern", required=True, | |
HELP = "RegEXP để lọc tên tệp."]="regexp to filter file names."] | |
args = Parser.parse_args [] = parser.parse_args[] | |
mẫu = re.compile [args.potype] = re.compile[args.pattern] | |
file_to_process = [] = [] | |
forpathinargs.paths: path in args.paths: | |
Forroot, Dirs, FileSinos.Walk [Path]: root, dirs, files in os.walk[path]: | |
Forfinfiles: f in files: | |
ifpotype.match [f] isnotnone: pattern.match[f] is not None: | |
abs_path = os.path.join [root, f] = os.path.join[root, f] | |
file_to_process.append [abs_path].append[abs_path] | |
iflen [files_to_process] == 0: len[files_to_process] == 0: | |
In "Không có tệp để xử lý." "No files to process." | |
Thoát [0][0] | |
forfinfiles_to_process: f in files_to_process: | |
printf f | |
S = Raw_Input ["Tệp ở trên sẽ được xử lý, tiếp tục? Y/N:"] = raw_input["Files above will be processed, continue? y/n: "] | |
ifs! = "y": s != "y": | |
print"Canceled." "Canceled." | |
Thoát [0][0] | |
forfinfiles_to_process: = args.header[0].read[] | |
printf p in files_to_process: | |
S = Raw_Input ["Tệp ở trên sẽ được xử lý, tiếp tục? Y/N:"] open[p, "r+b"] as f: | |
ifs! = "y": u"Processing {}".format[p] | |
tiêu đề = args.header [0] .read [] = f.read[] | |
forpinfiles_to_process:.seek[0, os.SEEK_SET] | |
withopen [p, "r+b"] ASF:.write[header] | |
printu "Xử lý {}". Định dạng [P].write[content] | |
nội dung = f.read [] "All done." |