Skip to content

Commit

Permalink
ExerciciosDeArquivos
Browse files Browse the repository at this point in the history
Exercício 01.
  • Loading branch information
hebert-santana committed Mar 25, 2023
1 parent 64141f4 commit 74acbdb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 07 - Exercícios com Arquivos/01_valida-IP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Faça um programa que leia um arquivo texto contendo uma lista de endereços IP e gere um outro arquivo, contendo um relatório dos endereços IP válidos e inválidos.

# Um endereço IP válido deve conter exatamente 4 partes, separadas por pontos.
# Cada parte deve ser um número inteiro entre 0 e 255.

# verifica se um endereço IP é válido (True / False)
def verificaIP(ip):
parts = ip.split('.')
if len(parts) != 4:
return False
for part in parts:
if not part.isdigit() or int(part) > 255:
return False
return True


# lê o arquivo de entrada
with open('enderecos.txt', 'r') as f:
ips = [line.strip() for line in f]

# separa os IPs validos e invalidos
valid_ips = []
invalid_ips = []
for ip in ips:
if verificaIP(ip):
valid_ips.append(ip)
else:
invalid_ips.append(ip)

# escreve o relatorio no arquivo de saida
with open('relatorio.txt', 'w') as f:
f.write('[Enderecos validos:]\n')
for ip in valid_ips:
f.write(ip + '\n')
f.write('\n[Enderecos invalidos:]\n')
for ip in invalid_ips:
f.write(ip + '\n')
8 changes: 8 additions & 0 deletions enderecos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
200.135.80.9
192.168.1.1
8.35.67.74
257.32.4.5
85.345.1.2
1.2.3.4
9.8.234.5
192.168.0.256
11 changes: 11 additions & 0 deletions relatorio.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Enderecos validos:]
200.135.80.9
192.168.1.1
8.35.67.74
1.2.3.4
9.8.234.5

[Enderecos invalidos:]
257.32.4.5
85.345.1.2
192.168.0.256

0 comments on commit 74acbdb

Please sign in to comment.