我打开一些文件:
myfilepath = "/home/colin/somefile.csv"
fid = open(myfilepath, "r")
Run Code Online (Sandbox Code Playgroud)
我想从fid
. 我可以用:
fid.name
Run Code Online (Sandbox Code Playgroud)
这将返回如下内容:
"<file /home/colin/somefile.csv>
Run Code Online (Sandbox Code Playgroud)
然而,据我所知,这并没有记录在案,所以我不能保证这个解决方案是面向未来的。
所以,我的问题是,是否有一种安全的方法可以IOStream
从IOStream
自身检索打开的文件路径?
目前 Julia 基础中不存在这样的函数。这可以通过查看为该类型定义的函数的完整列表来确定IOStream
:
julia> methodswith(IOStream)\n[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\n[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\n[3] bytesavailable(s::IOStream) in Base at iostream.jl:377\n[4] close(s::IOStream) in Base at iostream.jl:61\n[5] eof(s::IOStream) in Base at iostream.jl:232\n[6] fd(s::IOStream) in Base at iostream.jl:55\n[7] filesize(s::IOStream) in Base at iostream.jl:222\n[8] flush(s::IOStream) in Base at iostream.jl:66\n[9] isopen(s::IOStream) in Base at iostream.jl:59\n[10] isreadable(s::IOStream) in Base at iostream.jl:75\n[11] iswritable(s::IOStream) in Base at iostream.jl:73\n[12] peek(s::IOStream, ::Type{UInt8}) in Base at iostream.jl:568\n[13] position(s::IOStream) in Base at iostream.jl:216\n[14] read(s::IOStream) in Base at iostream.jl:516\n[15] read(s::IOStream, ::Type{UInt8}) in Base at iostream.jl:396\n[16] read(s::IOStream, T::Union{Type{Int16}, Type{Int32}, Type{Int64}, Type{UInt16}, Type{UInt32}, Type{UInt64}}) in Base at iostream.jl:405\n[17] read(s::IOStream, ::Type{Float16}) in Base at iostream.jl:417\n[18] read(s::IOStream, ::Type{Float32}) in Base at iostream.jl:418\n[19] read(s::IOStream, ::Type{Float64}) in Base at iostream.jl:419\n[20] read(s::IOStream, nb::Integer; all) in Base at iostream.jl:557\n[21] readavailable(s::IOStream) in Base at iostream.jl:379\n[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\n[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\n[24] readline(s::IOStream; keep) in Base at iostream.jl:444\n[25] readuntil(s::IOStream, delim::UInt8; keep) in Base at iostream.jl:435\n[26] redirect_stderr(handle::Union{IOStream, Base.LibuvStream}) in Base at stream.jl:1148\n[27] redirect_stdin(handle::Union{IOStream, Base.LibuvStream}) in Base at stream.jl:1148\n[28] redirect_stdout(handle::Union{IOStream, Base.LibuvStream}) in Base at stream.jl:1148\n[29] seek(s::IOStream, n::Integer) in Base at iostream.jl:127\n[30] seekend(s::IOStream) in Base at iostream.jl:161\n[31] show(io::IO, s::IOStream) in Base at iostream.jl:35\n[32] skip(s::IOStream, delta::Integer) in Base at iostream.jl:184\n[33] stat(s::IOStream) in Base at iostream.jl:57\n[34] take!(s::IOStream) in Base at iostream.jl:432\n[35] truncate(s::IOStream, n::Integer) in Base at iostream.jl:106\n[36] unsafe_read(s::IOStream, p::Ptr{UInt8}, nb::UInt64) in Base at iostream.jl:422\n[37] unsafe_write(s::IOStream, p::Ptr{UInt8}, nb::UInt64) in Base at iostream.jl:371\n[38] write(s::IOStream, b::UInt8) in Base at iostream.jl:366\n
Run Code Online (Sandbox Code Playgroud)\n其中与您要查找的内容最接近的是stat
,它可以为您提供基础文件的索引节点,但不能提供文件路径本身。
help?> stat\nsearch: stat lstat @static startswith stacktrace StackTraces @fastmath ignorestatus InvalidStateException abstract AbstractSet AbstractChar AbstractDict abstract type AbstractFloat AbstractArray AbstractRange AbstractMatch\n\n stat(file)\n\n Returns a structure whose fields contain information about the file. The fields of the structure are:\n\n Name Description\n \xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93 \xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\xe2\x80\x93\n size The size (in bytes) of the file\n device ID of the device that contains the file\n inode The inode number of the file\n mode The protection mode of the file\n nlink The number of hard links to the file\n uid The user id of the owner of the file\n gid The group id of the file owner\n rdev If this file refers to a device, the ID of the device it refers to\n blksize The file-system preferred block size for the file\n blocks The number of such blocks allocated\n mtime Unix timestamp of when the file was last modified\n ctime Unix timestamp of when the file was created\n
Run Code Online (Sandbox Code Playgroud)\n