如何从打开命令中查找文件路径

发布于 2024-12-04 10:26:52 字数 222 浏览 4 评论 0原文

我需要获取 fo 变量中的文件路径,以便可以将路径传递给 unzip_file 函数。我如何获得这里的路径?

url = 'http://www.dtniq.com/product/mktsymbols_v2.zip'
open(url, 'r') do |fo|
  puts "unzipfile "
  unzip_file(fo, "c:\\temp11\\")
end

I need to get the path of the file in fo variable so that i can pass the path to the unzip_file function. how do i get the path here?

url = 'http://www.dtniq.com/product/mktsymbols_v2.zip'
open(url, 'r') do |fo|
  puts "unzipfile "
  unzip_file(fo, "c:\\temp11\\")
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2024-12-11 10:26:52

至于如何做到这一点,我会这样做:

  1. 找出我正在处理的对象的类

    <前><代码>ruby-1.9.2-p290 :001> tmp_file = open('tmp.txt', 'r')
    => #<文件:tmp.txt>
    ruby-1.9.2-p290:001> tmp_file.class
    =>文件

  2. 去查找该类的文档

    Google 搜索:ruby 文件

    返回Class: File ruby​​-doc.org =>; www.ruby-doc.org/core/classes/File.html

  3. 查看方法。有一个叫做 path ->看起来很有趣

如果我现在还没有找到答案,那么

  1. 继续在 google/stack Overflow 上查找一下
  2. 我真的找不到符合我的问题的解决方案。是时候在这里提问了

大多数时候 1..3 应该可以满足您的需求。一旦您学会阅读文档,您就可以更快地完成工作。它只是试图克服刚开始时进入文档的困难。

In terms of how to do it I would do this:

  1. Find out the class of the object I am dealing with

    ruby-1.9.2-p290 :001 > tmp_file = open('tmp.txt', 'r')
      => #<File:tmp.txt> 
    ruby-1.9.2-p290 :001 > tmp_file.class
      => File
    
  2. Go look up the documentation for that class

    Google Search : ruby file

    Which returns Class: File ruby-doc.org => www.ruby-doc.org/core/classes/File.html

  3. Look at the methods. There is one called path -> looks interesting

If I haven't found an answer by now then

  1. Continue looking around google/stack overflow for a bit
  2. I really can't find a solution that matches my problem. Time to ask a question on here

Most of the time 1..3 should get you what you need. Once you learn to read the documentation you can do things a lot quicker. It's just trying to overcome how difficult it is to get into the docs when you first start.

空宴 2024-12-11 10:26:52

块中的 fo 应该是 Tempfile 因此您可以使用 路径方法:

url = 'http://www.dtniq.com/product/mktsymbols_v2.zip'
open(url, 'r') do |fo|
    puts "unzipfile "
    unzip_file(fo.path, "c:\\temp11\\")
end

The fo in your block should be a Tempfile so you can use the path method:

url = 'http://www.dtniq.com/product/mktsymbols_v2.zip'
open(url, 'r') do |fo|
    puts "unzipfile "
    unzip_file(fo.path, "c:\\temp11\\")
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文