1 ImportOS2 ImportSYS3 4 deffind_file (Root_dir, type):5Dirs_pool =[Root_dir]6Dest_pool = []7 8 defscan_dir (directory):9Entries =os.walk (directory)Ten forRoot, dirs, filesinchEntries: OneDirs_pool.extend ([Os.path.join (Root, Dir_entry) forDir_entryinchdirs]) A forFile_entryinchFiles: - ifTypeinchSTR (file_entry) [-len (type):]: -Dest_pool.append ("'. Join (Os.path.join (root, File_entry ))) the - whileDirs_pool: - Scan_dir (Dirs_pool.pop ()) - returnDest_pool + - defgen_ps (Root_dir, type): +Vim_cmd ='Vim-me-e-C ": hardcopy >%.ps"-C ": Q"' APs2pdf_cmd ='ps2pdf {filename}.ps {filename}.pdf' atDests =find_file (Root_dir, type) - if notdests: - return - Print 'found these source code files:' - forDestinchdests: - Printdest in Print 'begin generate PS files!' - forDestinchdests: toCommand ="'. Join ([Vim_cmd, dest]) + PrintCommand - os.system (command) the forDestinchdests: *Command = Ps2pdf_cmd.format (filename=dest) $ PrintCommandPanax Notoginseng os.system (command) - the if __name__=='__main__': + ifLen (sys.argv) = = 3: AGen_ps (sys.argv[1], sys.argv[2]) the Else: + Print 'usage:python code2pdf.py Directory filetype\n' - 'such As:python code2pdf.py/home/bruce/python. PY'
Convert source code to PDF in Python