-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8109002
commit fbc41cd
Showing
14 changed files
with
979 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Lembre-se de alterar o caminho do arquivo, para o caminho completo da sua máquina! | ||
|
||
arquivo = open( | ||
"/home/guilherme/Projetos/dio/codigo-fonte/trilha-python-dio/05 - Manipulação de arquivos/lorem.txt", "r" | ||
) | ||
print(arquivo.read()) | ||
arquivo.close() | ||
|
||
arquivo = open( | ||
"/home/guilherme/Projetos/dio/codigo-fonte/trilha-python-dio/05 - Manipulação de arquivos/lorem.txt", "r" | ||
) | ||
print(arquivo.readline()) | ||
arquivo.close() | ||
|
||
arquivo = open( | ||
"/home/guilherme/Projetos/dio/codigo-fonte/trilha-python-dio/05 - Manipulação de arquivos/lorem.txt", "r" | ||
) | ||
print(arquivo.readlines()) | ||
arquivo.close() | ||
|
||
arquivo = open( | ||
"/home/guilherme/Projetos/dio/codigo-fonte/trilha-python-dio/05 - Manipulação de arquivos/lorem.txt", "r" | ||
) | ||
# tip | ||
while len(linha := arquivo.readline()): | ||
print(linha) | ||
|
||
arquivo.close() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
arquivo = open( | ||
"/home/guilherme/Projetos/dio/codigo-fonte/trilha-python-dio/05 - Manipulação de arquivos/teste.txt", "w" | ||
) | ||
arquivo.write("Escrevendo dados em um novo arquivo.") | ||
arquivo.writelines(["\n", "escrevendo", "\n", "um", "\n", "novo", "\n", "texto"]) | ||
arquivo.close() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
ROOT_PATH = Path(__file__).parent | ||
|
||
os.mkdir(ROOT_PATH / "novo-diretorio") | ||
|
||
arquivo = open(ROOT_PATH / "novo.txt", "w") | ||
arquivo.close() | ||
|
||
os.rename(ROOT_PATH / "novo.txt", ROOT_PATH / "alterado.txt") | ||
|
||
os.remove(ROOT_PATH / "alterado.txt") | ||
|
||
shutil.move(ROOT_PATH / "novo.txt", ROOT_PATH / "novo-diretorio" / "novo.txt") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from pathlib import Path | ||
|
||
ROOT_PATH = Path(__file__).parent | ||
|
||
|
||
try: | ||
arquivo = open(ROOT_PATH / "novo-diretorio" / "novo.txt", "r") | ||
except FileNotFoundError as exc: | ||
print("Arquivo não encontrado!") | ||
print(exc) | ||
except IsADirectoryError as exc: | ||
print(f"Não foi possível abrir o arquivo: {exc}") | ||
except IOError as exc: | ||
print(f"Erro ao abrir o arquivo: {exc}") | ||
except Exception as exc: | ||
print(f"Algum problema ocorreu ao tentar abrir o arquivo: {exc}") | ||
|
||
|
||
# try: | ||
# arquivo = open(ROOT_PATH / "novo-diretorio") | ||
# except IsADirectoryError as exc: | ||
# print(f"Não foi possível abrir o arquivo: {exc}") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from pathlib import Path | ||
|
||
ROOT_PATH = Path(__file__).parent | ||
|
||
try: | ||
with open(ROOT_PATH / "1lorem.txt", "r") as arquivo: | ||
print(arquivo.read()) | ||
except IOError as exc: | ||
print(f"Erro ao abrir o arquivo {exc}") | ||
|
||
|
||
# try: | ||
# with open(ROOT_PATH / "arquivo-utf-8.txt", "w", encoding="utf-8") as arquivo: | ||
# arquivo.write("Aprendendo a manipular arquivos utilizando Python.") | ||
# except IOError as exc: | ||
# print(f"Erro ao abrir o arquivo {exc}") | ||
|
||
try: | ||
with open(ROOT_PATH / "arquivo-utf-8.txt", "r", encoding="utf-8") as arquivo: | ||
print(arquivo.read()) | ||
except IOError as exc: | ||
print(f"Erro ao abrir o arquivo {exc}") | ||
except UnicodeDecodeError as exc: | ||
print(exc) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import csv | ||
from pathlib import Path | ||
|
||
ROOT_PATH = Path(__file__).parent | ||
|
||
COLUNA_ID = 0 | ||
COLUNA_NOME = 1 | ||
|
||
|
||
try: | ||
with open(ROOT_PATH / "usuarios.csv", "w", newline="", encoding="utf-8") as arquivo: | ||
escritor = csv.writer(arquivo) | ||
escritor.writerow(["id", "nome"]) | ||
escritor.writerow(["1", "Maria"]) | ||
escritor.writerow(["2", "João"]) | ||
except IOError as exc: | ||
print(f"Erro ao criar o arquivo. {exc}") | ||
|
||
|
||
try: | ||
with open(ROOT_PATH / "usuarios.csv", "r", newline="", encoding="utf-8") as arquivo: | ||
leitor = csv.reader(arquivo) | ||
for idx, row in enumerate(leitor): | ||
if idx == 0: | ||
continue | ||
print(f"ID: {row[COLUNA_ID]}") | ||
print(f"Nome: {row[COLUNA_NOME]}") | ||
except IOError as exc: | ||
print(f"Erro ao criar o arquivo. {exc}") | ||
|
||
|
||
try: | ||
with open(ROOT_PATH / "usuarios.csv", newline="") as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row in reader: | ||
print(f"ID: {row['id']}") | ||
print(f"Nome: {row['nome']}") | ||
except IOError as exc: | ||
print(f"Erro ao criar o arquivo. {exc}") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Aprendendo a manipular arquivos utilizando Python.ÿÿÿ |
Oops, something went wrong.