Last active
March 1, 2020 17:21
-
-
Save Bundi-py/6c775b56779c2620e21a971ed595501a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# Provera da su dati svi argumenti. | |
if len(sys.argv) != 3: | |
print("Unesi ime 2 fajla u komandnoj liniji.") | |
quit() | |
try: | |
novi = open(sys.argv[2], "w") | |
with open(sys.argv[1], 'r') as f: | |
for i, line in enumerate(f, start=1): | |
novi.writelines(('{}: {}'.format(i, line))) | |
except: | |
print("Dogodila se greška pri učitavanju fajla.") | |
quit() | |
******************************* | |
Pantino rešenje, sa dve opcije. | |
Program numeriše svaki red ili | |
samo redove sa tekstom. Prazne preskače | |
import sys | |
import argparse | |
import fileinput | |
parser = argparse.ArgumentParser(description='Numeriši redove u tekstu.') | |
parser.add_argument('--numsve', action='store_true', help='numeriši sve redove') | |
parser.add_argument("files", nargs=argparse.REMAINDER) | |
args = parser.parse_args() | |
# Provera da su dati svi argumenti. | |
if len(args.files) != 3: | |
print("Unesi naziv 2 ulazna i 1 izlaznog fajla u komandnoj liniji.") | |
parser.print_help() | |
sys.exit() | |
broj = 1 | |
novi = open(args.files[2], 'w') | |
try: | |
for line in fileinput.input(files=args.files[:2]): | |
if args.numsve: | |
novi.write(f'{broj}: {line}') | |
broj+=1 | |
else: | |
if line.strip() == '': | |
novi.write(line) | |
else: | |
novi.write(f'{broj}: {line}') | |
broj+=1 | |
except FileNotFoundError as err: | |
print(f'No such file: {err.filename}') | |
parser.print_help() | |
finally: | |
novi.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment