Skip to content

Commit ed4cc33

Browse files
authored
Add warning prompt for File write operations (#13557)
1 parent e5bbc73 commit ed4cc33

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/elixir/pages/getting-started/io-and-the-file-system.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ hello world
2727

2828
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:
2929

30+
> #### Potential data loss warning {: .warning}
31+
>
32+
> The following code opens a file for writing. If an existing file is available at the given path, its contents will be deleted.
33+
3034
```elixir
3135
iex> {:ok, file} = File.open("path/to/file/hello", [:write])
3236
{:ok, #PID<0.47.0>}
@@ -38,7 +42,7 @@ iex> File.read("path/to/file/hello")
3842
{:ok, "world"}
3943
```
4044

41-
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.
45+
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.
4246

4347
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).
4448

@@ -96,7 +100,7 @@ With this, we have covered the main modules that Elixir provides for dealing wit
96100
You may have noticed that `File.open/2` returns a tuple like `{:ok, pid}`:
97101

98102
```elixir
99-
iex> {:ok, file} = File.open("hello", [:write])
103+
iex> {:ok, file} = File.open("hello")
100104
{:ok, #PID<0.47.0>}
101105
```
102106

0 commit comments

Comments
 (0)