Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Warning Prompt for File Write Operations in Elixir Learning Documentation #13557

Merged
merged 3 commits into from
May 14, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/elixir/pages/getting-started/io-and-the-file-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

The `File` module contains functions that allow us to open files as IO devices. By default, files are opened in binary mode, which requires developers to use the specific `IO.binread/2` and `IO.binwrite/2` functions from the `IO` module:

> #### Potential data loss warning {: .warning}
>

Check failure on line 31 in lib/elixir/pages/getting-started/io-and-the-file-system.md

View workflow job for this annotation

GitHub Actions / Lint Markdown content

Trailing spaces

lib/elixir/pages/getting-started/io-and-the-file-system.md:31:2 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md
> The following code opens a file for writing. If an existing file is available at the given path, its contents will be deleted.

```elixir
iex> {:ok, file} = File.open("path/to/file/hello", [:write])
{:ok, #PID<0.47.0>}
Expand All @@ -38,7 +42,7 @@
{:ok, "world"}
```

A file can also be opened with `:utf8` encoding, which tells the `File` module to interpret the bytes read from the file as UTF-8-encoded bytes.
The file could be opened with the `:append` option, instead of `:write`, to preserve its contents. You may also pass the `:utf8` option, which tells the `File` module to interpret the bytes read from the file as UTF-8-encoded bytes.

Besides functions for opening, reading and writing files, the `File` module has many functions to work with the file system. Those functions are named after their UNIX equivalents. For example, `File.rm/1` can be used to remove files, `File.mkdir/1` to create directories, `File.mkdir_p/1` to create directories and all their parent chain. There are even `File.cp_r/2` and `File.rm_rf/1` to respectively copy and remove files and directories recursively (i.e., copying and removing the contents of the directories too).

Expand Down Expand Up @@ -96,7 +100,7 @@
You may have noticed that `File.open/2` returns a tuple like `{:ok, pid}`:

```elixir
iex> {:ok, file} = File.open("hello", [:write])
iex> {:ok, file} = File.open("hello")
{:ok, #PID<0.47.0>}
```

Expand Down
Loading