file - 如何从 Julia 中打开的 IOStream 中检索文件路径

标签 file io julia

我打开一些文件:

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

我想从 fid 检索文件路径.我可以用:
fid.name

这将返回如下内容:
"<file /home/colin/somefile.csv>

然而,据我所知,这并没有记录在案,所以我不能保证这个解决方案是面向 future 的。

所以,我的问题是,是否有一种安全的方法来检索打开的文件路径 IOStream来自 IOStream本身?

最佳答案

目前,基本 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

关于file - 如何从 Julia 中打开的 IOStream 中检索文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62064025/

相关文章:

c# - 文件被两个进程使用

arrays - 如何在 Julia 中获取数组的大小?

git - 如何对整个目录应用 git --skip-worktree ?

python - 我正在尝试将数据写入文件

ruby - 需要 ruby​​ 目录中所有文件的最佳方法?

python - 文件操作在相对路径中不起作用

java - 无法使用 FileInputStream 复制 PDF 文件

linux - 在 Bash 中循环文件的内容

julia - 什么是 JuliaLang 等同于 Python 的 sys.executable?

julia - 如何在 Julia 表达式中使用混合模型的 "|"给定符号