Julia: Check isdir(dest) and copy into that directory in cp and friends?

Created on 12 Mar 2018  路  7Comments  路  Source: JuliaLang/julia

Should we check if "dest" is a directory, and if so, copy "src" to that directory?

julia> mkdir("dest"); touch("src"); cp("src", "dest")
ERROR: ArgumentError: 'dest' exists. `force=true` is required to remove 'dest' before copying.
Stacktrace:
 [1] #checkfor_mv_cp_cptree#10(::Bool, ::Function, ::String, ::String, ::String) at ./file.jl:191
 [2] #checkfor_mv_cp_cptree at ./<missing>:0 [inlined]
 [3] #cp#12(::Bool, ::Bool, ::Nothing, ::Function, ::String, ::String) at ./file.jl:241
 [4] cp(::String, ::String) at ./file.jl:236
 [5] top-level scope

Compare e.g. with

fredrik@fredrik-laptop:~$ mkdir dest && touch src && cp src dest && ls dest
src
filesystem

Most helpful comment

cp(src, into=dir) doesn't seem that much better than cp(src, joinpath(dir, src)). (If somebody needs this function a lot they could always define a cpinto(src, dir) = cp(src, joinpath(dir, src)) function.)

This use-case would have to come up a lot to put this specific functionality in to Base. Grepping Julia packages for uses of cp with joinpath, I see a handful of code that could be marginally shortened with an into= keyword, but it doesn't seem like enough to be worthwhile.

All 7 comments

Adding triage since this changes behavior and probably needs to be decided before 1.0.

What do other languages do? I would think it's reasonable to be stricter in a programming language than in the shell, which allows catching potential mistakes.

Just noting also that the current behaviour is not very useful; it is unlikely that you want to replace a folder with a file (which is the result of setting force = true in the call above), and that, what you currently have to do

cp("src", joinpath(dir, "src"))

is not very nice.

Python has both copyfile (with our current semantics) and copy (which supports directory targets). Ruby is similar with copy_file and cp.

Would cp(src, into=dir) be a good API here?

I think changing this would be worse in terms of orthogonality --- if you actually want the logic "if this is a directory then copy into it", then you should write that. Conditional behavior should not be built into the library function itself.

cp(src, into=dir) doesn't seem that much better than cp(src, joinpath(dir, src)). (If somebody needs this function a lot they could always define a cpinto(src, dir) = cp(src, joinpath(dir, src)) function.)

This use-case would have to come up a lot to put this specific functionality in to Base. Grepping Julia packages for uses of cp with joinpath, I see a handful of code that could be marginally shortened with an into= keyword, but it doesn't seem like enough to be worthwhile.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sbromberger picture sbromberger  路  3Comments

wilburtownsend picture wilburtownsend  路  3Comments

dpsanders picture dpsanders  路  3Comments

musm picture musm  路  3Comments

Keno picture Keno  路  3Comments