# Closing files when using CSV.Rows

**URL:** https://discourse.julialang.org/t/closing-files-when-using-csv-rows/29169
**Category:** General Usage
**Tags:** question, csv
**Created:** [September 25, 2019, 3:58pm UTC](https://discourse.julialang.org/t/closing-files-when-using-csv-rows/29169 "2019-09-25T15:58:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 25, 2019, 3:58pm UTC](https://discourse.julialang.org/t/closing-files-when-using-csv-rows/29169/1 "2019-09-25T15:58:09Z")

</div>

I’m using `CSV.Rows` to iterate through a very large CSV file. In base Julia, files that are opened with `open` should be closed with `close` (or you can use the `do` syntax instead). However, after perusing the CSV.jl docs, there doesn’t appear to be a `close` method for file iterators created with `CSV.Rows`. How does CSV.jl ensure that files are closed when they’re done being used? Does it just rely on the files being closed when the `CSV.Rows` object gets garbage collected?

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [September 27, 2019, 9:14pm UTC](https://discourse.julialang.org/t/closing-files-when-using-csv-rows/29169/2 "2019-09-27T21:14:27Z")

</div>

Cc: @quinnj

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [October 3, 2019, 1:59am UTC](https://discourse.julialang.org/t/closing-files-when-using-csv-rows/29169/3 "2019-10-03T01:59:01Z")

</div>

Sorry for the slow response here, and thanks @nalimilan for the ping. There’s a few different paths here depending on how you use `CSV.Rows`:

- If you pass the file as a string, like `CSV.Rows("filename.csv")`, then CSV.jl will mmap the file by default, which indeed then relies on the `CSV.Rows` object getting garbage collected to unmmap the file buffer
- You can also pass in your own opened file, like `io = open("filename.csv"); CSV.Rows(io)`, which currently will “slurp” the entire file into a mmapped buffer to be used while reading rows. So you would need to close the `io` yourself afterwards

Hopefully that helps!

---

<div class="post-metadata">

### Author: ![Davide\_DelVento](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide_delvento/32/52717_2.png) [@Davide\_DelVento](https://discourse.julialang.org/u/Davide_DelVento)
#### Post date: [September 9, 2024, 11:37pm UTC](https://discourse.julialang.org/t/closing-files-when-using-csv-rows/29169/4 "2024-09-09T23:37:20Z")

</div>

This applies also to `CSV.write`, correct?

I thought those would be immediately closed (since they are writing a full table, not a single row), but looking at its source code it does not appear to be so.

Reason for asking: debugging a problem and understanding if a file is closed immediately after the function call, or rather later when the interpreter has time to run the garbage collector would help tremendously.

Thanks!
