Foreground
Foreground
D0Z - motion / graphic / dev๐Ÿงบ

Brain ๐Ÿ”’

โœ•

brain

CAUTION

Some files are private or not ready yet, if a link gives you a 404 that's why :)
This is a work in progress, new notes will be added daily

List of note Indexes


                .... |\| .|||.    |\/|  .|\\. .|.
              .\\||/|.  .\|..||  .\|..|||...||.....
            .|   .....||.|\\\\\\    ... |\\\/. .\\. .||
        .\\|   .|\\|..|\\ .\\\\  .\\|. .\\\\\|  |\| |\\|
        |\\  |\\|..  /\\\. |\|\\   \\\\|.   .|\\. .\\\\|
        .\\. |\\\\\\\|.. .\\|      .\\||\\/|. |\\|     .|/\|
      \. .\\\||/\|....   \\| |\/   |\\\  |\\\| .\\|  \\| ....
    |\/  |\\| |\\\\\\\| |\|.\\\   \\\\. |\\\\| |\\  \\.  ||..
      ..|||\|  .. .\\\\. |\\\\\\  .\\|....|\\\. |\\\\\\  |/. |/
    \\\|..   .\\\ |\\. .\\\| \\|   |||\\\.  ...|\\/||.  .\\\
  |\|  |\/.  /\\\\\| .\\\\. \\.   .../|.   .|\\\|.   |\\\|  |\\
    .  |||\\|.  ..||| .\\|.  |\|  \\|..../\\| |\\|  .\\\\\   ..
    \\|  .||\|.|..      |\\\|./\  \\||\. ||...    .\\\\/.  |\/||
    ..|/||.   |\\\\\|.     .|/\|  ||.        .||\\\     .|||. |\
  .\/|  \\\. .|\\\|||\\\||..       .|\\\\\\\|\\\\.  /\\\\||. |
    .|| |\\\\|  .......|\\\\\\.  .\\\. ..|||....  .\\\\|.. ...
    \\||....|\\\\\\\\||\\\\\\\/   |\|....|||\\\\\\\/....|\\\|
      ||.|\||\\| .....|\\\\..\\|    .|\\\. ||........ .|\|...
          . |\  /\\/\\\\\\. |\.  .\\\\\\.  ./\\\\/  |\\\||.
        .|\.|\| .|\|... ..              ./\\|..|\\|  ..
            .|\\|  .\\\\.\\\\\\\   \||...\\\|..|\\\|.||
                |\. ..||....||     ... ..||\\\. .|.
                        ...            .......

โ†‘ Python


Python - Read and Write Files

[!WARNING] Remember to close a file after opening it or use with (see bellow)

file = open("file_name.ext", mode)
file.close()

There are four modes for opening a file:

  • r: Read - default, open a file for reading, error if it does not exist
  • w: Write - overwrites a file if it exists if not creates it
  • a: Append - appends at the end of the file without overwriting it, creates it if does not exist
  • x: Create - create a file, error if it already exists

You can also specify if the file should be handle as binary or text (default) using b or t

file = open("file_name.ext", "rt")
file.close()

Read entirely a file

file = open("my_file.md")
content = file.read()
file.close()

Read line by line with a loop

file = open("my_file.md")
for line in file:
  print(line, end=" ") # end=" " avoid doubles line breaks
file.close()

Read a limited number of characters

file = open("my_file.md")
content = file.read(20)
file.close()

Read multiple lines

file = open("my_file.md")
content = file.readlines() # returns a list of lines
for line in content:
  print(line.strip()) # strip() to remove spaces and line breaks
file.close()

Write in a file

  • write(): writes a string in a file
  • writelines(): writes using a list of string (one per line) in a file
# Write
file = open("my_file.md", "w")
file.write("Hello world!\n")
file.write("Second line")
file.write("Third line\nFourth line")
file.write("""Block of text
New line
  Tabulation
""")

# Write lines
lines = ["First line\n", "Second line"]
file.writelines(lines)
file.close()

# Append
file = open("my_file.md", "a")
file.write("New line at the end of the file\n")
file.close()

Good practice: use with

Using with allows automatic file opening and closing, even if an error is raised.

with open("my_file.md") as file:
  content = file.read()
ยฉ 2024 d0z.eu | 0.1.0-beta.39