-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaula094_stringIO.py
More file actions
31 lines (21 loc) · 867 Bytes
/
aula094_stringIO.py
File metadata and controls
31 lines (21 loc) · 867 Bytes
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
# _*_ coding: utf-8 _*_
"""
StringIO
ATENÇÃO: Para ler ou escrever dados em arquivos do sistema operacional, o software precisa ter permissão:
- Permissão de leitura = Para ler o arquvo;
- Permissão de escrita = Para escrever o arquivo.
StringIO = Utilizado para ler e criar arquivos em memória.
"""
# Primeiro fazemos o import:
from io import StringIO
mensagem = 'Esta é apenas uma string normal'
# Podemos criar um arquivo em memória já com uma string inserida ou mesmo vazio para inserir texto depois:
arquivo = StringIO(mensagem)
# Isso que foi feito acima, é equivalente à: arquivo = open('arquivo.txt', 'w')
# Agora tendo o arquivo, podemos utilizar tudo o que já sabemos.
print(arquivo.read())
# Escrevendo outros texto
arquivo.write('\nOutro texto')
# Podemos inclusive movimentar o cursor
arquivo.seek(0)
print(arquivo.read())