我最近创建了一个程序来解压缩指定目录中的文件。在解压缩文件时,我收到错误:
/usr/local/lib/ruby/1.9.1/fileutils.rb:1423:in `stat': No such file or directory - /path/to/directory/file.file (Errno::ENOENT)
from /usr/local/lib/ruby/1.9.1/fileutils.rb:1423:in `block in fu_each_src_dest'
from /usr/local/lib/ruby/1.9.1/fileutils.rb:1437:in `fu_each_src_dest0'
from /usr/local/lib/ruby/1.9.1/fileutils.rb:1421:in `fu_each_src_dest'
from /usr/local/lib/ruby/1.9.1/fileutils.rb:504:in `mv'
from spreadsheet:97:in `block in unlock_archive_spreadsheet'
from spreadsheet:76:in `each'
from spreadsheet:76:in `unlock_archive_spreadsheet'
from spreadsheet:24:in `start'
from spreadsheet:111:in `<main>'
当我尝试在将要使用的目录中运行程序时,它会输出上述错误 我有点困惑,因为当我测试运行这个程序时,它的工作原理。例如:
[me@me ruby]$ ruby spreadsheet
Is this an archive spreadsheet?: y
Enter Folio # of spreadsheet if unlocking multiple use comma followed by space: 2719569
Text file containing the information is: filelist_20120625.txt
Continue: yes
Preparing to extract file..
Extracting file...
Moving file to 4000_UW_spreadsheets directory
File extracted and moved successfully
[me@me ruby]$
这是来源:
70 require 'fileutils'
71
72
73 def unlock_archive_spreadsheet
74 print "Enter Folio # of spreadsheet if unlocking multiple use comma followed by space: "
75 folio = gets.chomp
76 folio.split(', ').each do |archived_file|
77 FileUtils.chdir("path/where/file/is")
78
79 archived_text_file = `grep -lr #{archived_file}.ods *.txt`
80 tar_name = archived_text_file.match(/\d+/).to_s
81 puts "Text file containing the information is: #{archived_text_file}"
82 print "Continue: "
83 continue = gets.chomp.downcase
84 if continue.start_with?("y")
85 puts "Preparing to extract file.."
86
87 if File.exist?("/path/to/file/#{archived_file}.ods")
88 puts "ERROR FILE EXISTS. Delete or move file and try again"
89 raise "File: #{archived_file}.ods exists in directory. Delete or move file to continue, if file is not found within directory, contact admin."
90 next
91 else
92 puts "Extracting file..."
93 `tar -xvzf UW_archive_#{tar_name}.tgz path/to/extract/from/#{archived_file}.ods`
94 end
95 puts "Moving file to 4000_UW_spreadsheets directory"
96
97 FileUtils.mv "/path/to/file/#{archived_file}.ods", '/path/to/move/to'
98
99 puts "File extracted and moved successfully"
100 else
101 puts "Exiting.."
102 exit 1
103 end
104 end
105 end
我在这里做错了吗?
答案 0 :(得分:1)
如果您查看错误消息,那么这个操作无法正常运行。您的代码中仍有多个占位符路径。占位符显式显示在错误中。
答案 1 :(得分:1)
好的,你的问题是三件事之一:
extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(filename_here))
tar -xvzf UW_archive_#{tar_name}.tgz path/to/extract/from/#{archived_file}.ods
我不完全确定你在这里尝试做什么。但是如果你需要更改目录,并坚持使用bash,-C
将变为目录。这是对该命令的重写:tar -x -f UW_archive_#{tar_name}.tgz -C /path/to/change/to #{archived_file}.ods
-x
告诉bash从tar
中提取-f
告诉它这是文件名,而-C
表示将此文件名解压缩到此目录中。
关于我能想到的所有内容,Ruby几乎可以做所有bash所能做的事情,你所要做的就是为它找到gem或库。希望这有帮助!
修改强>
如果从tarball内部目录中的大型tarball中提取单个文件:tar -x -f tarname.tgz -C path/to/extract/to path/to/file/filename.file
那应该从tar中提取文件并进入指定的目录,但是要小心,这将使用路径提取文件ALONG,以摆脱你可以使用的路径,--strip-components 1
数字是要删除多少条路径。或者您可以从路径中移动文件并自行删除路径,希望这有帮助!