Skip to content

Files

Write and Read files

with open("example.txt", "r" "w" "a") as f: # para lectura del archivo
  content = f.read()
  print(content)

with open("example.txt", "r") as f: # para lectura del archivo mas optimo
  for line in f:
  print(line.strip())

with open("example.txt", "rb") as f: # para lectura del archivo mas optimo y en formato bites
  for line in f:
  print(line.strip().decode())

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

my_list = ["text\n", "text\n", "text\n" , "text\n"]

with open("example.txt", "w") as f:
  f.writelines(my_list)
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

try:
  with open("/Pictures/imagen.png", "rb") as f_in , open("imagenCopy.png", "wb") as f_out:
    file_content = f_in
    f_out.write(file_content)
except FileNotFoundError:
  print("file not found")