Scripting with Ruby : be careful with File.open

Once again, I spend a long time debugging something which was quite obvious (well, at least when I discovered the solution…)
Trying to copy all the files from a .m3u playlist file (in each line there is a file path), I used ruby (well bash and zsh are not as funny as ruby), and especially the FileUtils.cp function.
The code is as follow :

Well, my mistake was to forget to add the line :
string = line.chomp
because when you don’t, Ruby add the extra « \n » at the end of every line, and then FileUtils returns to you this very explicit message :

/usr/lib/ruby/1.8/fileutils.rb:1200:in stat': No such file or directory - (Errno::ENOENT)
from /usr/lib/ruby/1.8/fileutils.rb:1200:in
lstat'
from /usr/lib/ruby/1.8/fileutils.rb:1178:in stat'
from /usr/lib/ruby/1.8/fileutils.rb:1260:in
copy_file'
from /usr/lib/ruby/1.8/fileutils.rb:463:in copy_file'
from /usr/lib/ruby/1.8/fileutils.rb:383:in
cp'
from /usr/lib/ruby/1.8/fileutils.rb:1395:in fu_each_src_dest'
from /usr/lib/ruby/1.8/fileutils.rb:1409:in
fu_each_src_dest0'
from /usr/lib/ruby/1.8/fileutils.rb:1393:in fu_each_src_dest'
from /usr/lib/ruby/1.8/fileutils.rb:382:in
cp'
from /serveur/anthony/ruby/export_playlist_to_files.rb:12
from /serveur/anthony/ruby/export_playlist_to_files.rb:5:in `each'
from /serveur/anthony/ruby/export_playlist_to_files.rb:5

And believe me, it takes a while to debug… (irb was helful to solve this bug by the way !)

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée.