#!/usr/bin/env python3 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. import sys, os, random import CodeLIB def main(): length, filename = usr_args() if not length: length = CodeLIB.get_int("Length", min=1, max=6144, default=20) if not filename: filename = file_choose() list = file_open(filename) list = " ".join(list.split()) index = random.randint(length, len(list)) print(list[(index - length):index]) def usr_args(): try: CodeLIB.help("[length file]", arg=True) length = int(sys.argv[1]) filename = sys.argv[2] except IndexError: return None, None except ValueError as err: print("Error:", err) CodeLIB.help("[length file]") return length, filename def file_open(filename): lines = "" fh = None try: fh = open(filename) for line in fh: if line.strip(): lines += line except (IOError, OSError) as err: print(err) return "" finally: if fh is not None: fh.close() return lines def file_choose(): files = [x for x in os.listdir(".") if os.path.isfile(x)] for x in range(len(files)): print((x + 1), ": ", files[x], sep="") open_index = CodeLIB.get_int("Open file number", min=1, max=len(files)) - 1 print("Opening file", files[open_index]) return files[open_index] if __name__ == "__main__": main()