Last active
February 9, 2020 08:28
-
-
Save Bundi-py/da8083d357b78c6f034d2f8babb1745a to your computer and use it in GitHub Desktop.
Program učita tekstualni fajl sa komandne linije i proveri da li je dat argument (ime fajla). Ako nije, ispiše odgovarajuću poruku. Ako jeste, onda ispiše poslednjih deset redova teksta iz učitanog fajla.
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
# Prikaži poslednjih 10 linija učitanog tekstualnog fajla, pri čemu je ime fajla argument na comandnoj liniji, | |
# posle naziva programa. | |
# | |
import sys | |
BR_REDOVA = 10 | |
# Provera da je argument dat. | |
if len(sys.argv) != 2: | |
print("Unesi ime fajla u komandnoj liniji.") | |
quit() | |
try: | |
inf = open(sys.argv[1], "r") | |
redovi = [] | |
for red in inf: | |
redovi.append(red) | |
if len(redovi) > BR_REDOVA: | |
redovi.pop(0) | |
inf.close() | |
except: | |
print("Dogodila se greška pri učitavanju fajla.") | |
quit() | |
for red in redovi: | |
print(red, end="") | |
******************************** | |
# Unapređeno rešenje by Panta, | |
# za slučajeve kada ima manje redova | |
# fajlu nego što treba izlistati. | |
redovi = [] | |
red = 0 | |
done=False | |
with open('new1.txt', 'rb') as f: | |
f.seek(-2, 2) | |
while red < 5: | |
while f.read(1) != b'\n': | |
if f.tell() <= 1: | |
f.seek(0) | |
redovi.append(f.readline().decode()) | |
done=True | |
# break | |
else: | |
f.seek(-2, 1) | |
if done: | |
break | |
pos = f.tell() | |
redovi.append(f.readline().decode()) | |
f.seek(pos - 2) | |
red += 1 | |
print(*redovi[::-1], sep='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment