Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve file path from an open IOStream in Julia

Tags:

file

io

julia

I open some file:

myfilepath = "/home/colin/somefile.csv"
fid = open(myfilepath, "r")

I would like to retrieve the file path from fid. I can use:

fid.name

and this will return something like:

"<file /home/colin/somefile.csv>

However, as near as I can tell, this isn't documented, so I've got no guarantee this solution is future-proof.

So, my question is, is there a safe method to retrieve the file-path of an open IOStream from the IOStream itself?

like image 568
Colin T Bowers Avatar asked May 28 '20 11:05

Colin T Bowers


1 Answers

Currently no such function exists in base Julia. This can be determined by looking at the complete list of functions defined for the type IOStream:

julia> methodswith(IOStream)
[1] crc32c(io::IOStream) in CRC32c at /Applications/Julia-1.6.app/Contents/Resources/julia/share/julia/stdlib/v1.6/CRC32c/src/CRC32c.jl:50
[2] crc32c(io::IOStream, crc::UInt32) in CRC32c at /Applications/Julia-1.6.app/Contents/Resources/julia/share/julia/stdlib/v1.6/CRC32c/src/CRC32c.jl:50
[3] bytesavailable(s::IOStream) in Base at iostream.jl:377
[4] close(s::IOStream) in Base at iostream.jl:61
[5] eof(s::IOStream) in Base at iostream.jl:232
[6] fd(s::IOStream) in Base at iostream.jl:55
[7] filesize(s::IOStream) in Base at iostream.jl:222
[8] flush(s::IOStream) in Base at iostream.jl:66
[9] isopen(s::IOStream) in Base at iostream.jl:59
[10] isreadable(s::IOStream) in Base at iostream.jl:75
[11] iswritable(s::IOStream) in Base at iostream.jl:73
[12] peek(s::IOStream, ::Type{UInt8}) in Base at iostream.jl:568
[13] position(s::IOStream) in Base at iostream.jl:216
[14] read(s::IOStream) in Base at iostream.jl:516
[15] read(s::IOStream, ::Type{UInt8}) in Base at iostream.jl:396
[16] read(s::IOStream, T::Union{Type{Int16}, Type{Int32}, Type{Int64}, Type{UInt16}, Type{UInt32}, Type{UInt64}}) in Base at iostream.jl:405
[17] read(s::IOStream, ::Type{Float16}) in Base at iostream.jl:417
[18] read(s::IOStream, ::Type{Float32}) in Base at iostream.jl:418
[19] read(s::IOStream, ::Type{Float64}) in Base at iostream.jl:419
[20] read(s::IOStream, nb::Integer; all) in Base at iostream.jl:557
[21] readavailable(s::IOStream) in Base at iostream.jl:379
[22] readbytes!(s::IOStream, b::Union{SubArray{UInt8, var"#s828", var"#s827", I, true} where {var"#s828", var"#s827"<:(Array{UInt8, N} where N), I<:Union{Tuple{Vararg{Real, N} where N}, Tuple{AbstractUnitRange, Vararg{Any, N} where N}}}, Array{UInt8, N} where N}) in Base at iostream.jl:509
[23] readbytes!(s::IOStream, b::Union{SubArray{UInt8, var"#s826", var"#s825", I, true} where {var"#s826", var"#s825"<:(Array{UInt8, N} where N), I<:Union{Tuple{Vararg{Real, N} where N}, Tuple{AbstractUnitRange, Vararg{Any, N} where N}}}, Array{UInt8, N} where N}, nb; all) in Base at iostream.jl:509
[24] readline(s::IOStream; keep) in Base at iostream.jl:444
[25] readuntil(s::IOStream, delim::UInt8; keep) in Base at iostream.jl:435
[26] redirect_stderr(handle::Union{IOStream, Base.LibuvStream}) in Base at stream.jl:1148
[27] redirect_stdin(handle::Union{IOStream, Base.LibuvStream}) in Base at stream.jl:1148
[28] redirect_stdout(handle::Union{IOStream, Base.LibuvStream}) in Base at stream.jl:1148
[29] seek(s::IOStream, n::Integer) in Base at iostream.jl:127
[30] seekend(s::IOStream) in Base at iostream.jl:161
[31] show(io::IO, s::IOStream) in Base at iostream.jl:35
[32] skip(s::IOStream, delta::Integer) in Base at iostream.jl:184
[33] stat(s::IOStream) in Base at iostream.jl:57
[34] take!(s::IOStream) in Base at iostream.jl:432
[35] truncate(s::IOStream, n::Integer) in Base at iostream.jl:106
[36] unsafe_read(s::IOStream, p::Ptr{UInt8}, nb::UInt64) in Base at iostream.jl:422
[37] unsafe_write(s::IOStream, p::Ptr{UInt8}, nb::UInt64) in Base at iostream.jl:371
[38] write(s::IOStream, b::UInt8) in Base at iostream.jl:366

The closest of any of these to what you are looking for is stat, which can give you the inode of the underlying file, but not the file path per se.

help?> stat
search: stat lstat @static startswith stacktrace StackTraces @fastmath ignorestatus InvalidStateException abstract AbstractSet AbstractChar AbstractDict abstract type AbstractFloat AbstractArray AbstractRange AbstractMatch

  stat(file)

  Returns a structure whose fields contain information about the file. The fields of the structure are:

  Name    Description
  ––––––– ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  size    The size (in bytes) of the file
  device  ID of the device that contains the file
  inode   The inode number of the file
  mode    The protection mode of the file
  nlink   The number of hard links to the file
  uid     The user id of the owner of the file
  gid     The group id of the file owner
  rdev    If this file refers to a device, the ID of the device it refers to
  blksize The file-system preferred block size for the file
  blocks  The number of such blocks allocated
  mtime   Unix timestamp of when the file was last modified
  ctime   Unix timestamp of when the file was created
like image 103
cbk Avatar answered Nov 15 '22 10:11

cbk