-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFuncoes_e_arquivos.py
49 lines (40 loc) · 1.08 KB
/
Funcoes_e_arquivos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Este exercicio mostra alguns exemplos simples de funcoes
operando sobre um arquivo, mostrando duas funcoes de files
seek(), readline() e read()
"""
from sys import argv, exit
from os.path import exists
try:
script, input_file = argv
except ValueError:
if len(argv) > 2:
print("Voce excedeu a quantidade de argumentos!")
print("Resolvendo problema...")
script = argv[0]
input_file = argv[1]
else:
print("Nome do arquivo nao inserido!")
print("Saindo do programa...")
exit()
# Verifica se o arquivo existem,
# se existir ele retorna True
if exists(input_file):
def print_all(f):
print(f.read())
def rewind(f):
f.seek(0)
def print_a_line(line_number, f):
print(line_number, f.readline())
current_file = open(input_file)
print("Primeiro irei imprimir o arquivo todo!")
print_all(current_file)
print("Agora vamos retroceder, como uma especie de fita!")
rewind(current_file)
print("Agora vamo imprimir tres linhas!")
print_a_line(1, current_file)
print_a_line(2, current_file)
print_a_line(3, current_file)
else:
print("O arquivo",input_file,"nao existe!")
exit()