List non-empty files in a folder

351 views
Skip to first unread message

Charles Novaes de Santana

unread,
Oct 29, 2015, 11:22:42 AM10/29/15
to julia...@googlegroups.com
Hi julians,

I am using readdir() and filter!() to list files in a folder that match a given pattern (lets' say, to list all .txt files).

Now I would like to filter the files according to their sizes. I only want to list non-empty files within a folder, so my first shot is to list files which sizes are higher than zero bytes.

Some days ago there was a thread about listing the last modified file, that used map() and mtime(). Do you know something similar to list files by size?

Thanks for any tip!

Best,

Charles

--
Um axé! :)

--
Charles Novaes de Santana, PhD
http://www.imedea.uib-csic.es/~charles

Yichao Yu

unread,
Oct 29, 2015, 11:25:54 AM10/29/15
to Julia Users
stat[1] should have the info you need to write your own filter.

[1] http://julia.readthedocs.org/en/latest/stdlib/file/?highlight=stat#Base.stat

Charles Novaes de Santana

unread,
Oct 29, 2015, 11:25:56 AM10/29/15
to julia...@googlegroups.com
Just to give a code to work. My code now is:

folder=".";
files = filter!(r"\.txt$",readdir(folder));

Thanks!

Charles

Charles Novaes de Santana

unread,
Oct 29, 2015, 11:41:44 AM10/29/15
to julia...@googlegroups.com
Thanks! stat is great!! :)

My R-biased programming can solve the problem by using the following code:

minsize=100
folder=".";
files = filter!(r"\.txt$",readdir(folder))[map(filesize,filter!(r"\.txt$",readdir(folder))).>minsize]

It solves my problem. But I would appreciate if anyone has any other suggestion to do it in a Julia-style :)

Thanks a lot!

Best,

Charles

Yichao Yu

unread,
Oct 29, 2015, 11:54:07 AM10/29/15
to Julia Users
On Thu, Oct 29, 2015 at 11:41 AM, Charles Novaes de Santana
<charles...@gmail.com> wrote:
> Thanks! stat is great!! :)
>
> My R-biased programming can solve the problem by using the following code:
>
> minsize=100
> folder=".";
> files =
> filter!(r"\.txt$",readdir(folder))[map(filesize,filter!(r"\.txt$",readdir(folder))).>minsize]

I don't think `readdir` necessarily garentee returning the files in
the same order every time you call it on all platforms. The simplest
improvement is just to call filter twice but you could also just do

```
filter!(fname->(endswith(fname, ".txt") && filesize(fname) > minsize),
readdir(folder))
```

Note that you don't need a regular expression for endswith...

Stefan Karpinski

unread,
Oct 29, 2015, 1:30:18 PM10/29/15
to Julia Users
You can also use do-block syntax to make it a little easer to read:

files = filter!(readdir(folder)) do fname
    splitext(fname)[2] == "txt" && filesize(fname) >= minsize
end

Using the splitext function avoids the somewhat unlikely corner case of having a hidden file whose entire name is ".txt" which should probably not be considered to have the extension ".txt" even though it does technically end with that string.

Charles Novaes de Santana

unread,
Oct 29, 2015, 4:30:24 PM10/29/15
to julia...@googlegroups.com
Thank you both! :) These different ways of doing the same thing in Julia are quite useful (and funny! :))

Best,

Charles
Reply all
Reply to author
Forward
0 new messages