如何从Julia中的打开IO流中检索文件路径

8

我打开了一些文件:

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

我想从fid中获取文件路径。我可以使用以下代码:
fid.name

这将返回类似以下内容的结果:

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

然而,据我所知,这并没有被记录下来,因此我无法保证这种解决方案是面向未来的。

那么,我的问题是,是否有一种安全的方法可以从IOStream本身获取打开的文件路径?


我尝试了read(`readlink /proc/self/fd/$(fd(fp))`, String),但显然该命令的进程没有继承文件描述符... - phipsgabler
2
在大多数编程语言中,IOStream 是一个抽象的概念,不关心字节来自哪里。它可以是内存、网络套接字、磁盘等。因此,除非存在像 FileIOStream 这样的概念,否则我不会期望 IOStream 关心并知道数据实际上来自哪里。 - Przemyslaw Szufel
@PrzemyslawSzufel 很有意思。我之前并没有这样想过,但确实很有道理。我想我可以做一些类似于 struct FileIOStream ; filepath::String, io::IOStream ; end 的事情,并在其周围编写一些简单的功能。另一方面,这可能比在代码中传递 IOStream 和文件路径的元组更费力 :-) - Colin T Bowers
@phipsgabler readlink("/proc/self/fd/$(RawFD(fd(fp)))") 可能有效,但仅适用于Linux。 - Simon Byrne
1个回答

2

目前在Julia的基础版本中没有此类功能。这可以通过查看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

这些内容中最接近你想要的是stat,它可以给出底层文件的inode,但不能直接给出文件路径。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接