【问题标题】:Ruby FileUtils: copying fileRuby FileUtils:复制文件
【发布时间】:2014-01-10 15:46:06
【问题描述】:

您好,我尝试在 FileUtils 的帮助下复制文件

file_path = "C:/example/abc.txt"
destination_directory = "C:/destination"

FileUtils.cp(file_path, destination_directory)

当我尝试运行此代码时,出现错误can't convert Fixnum into String。 我哪里做错了?

谢谢!

【问题讨论】:

    标签: ruby windows


    【解决方案1】:

    您必须对 windows 路径使用反斜杠,然后将它们转义以使其成为文字,即

    file_path = "C:\\example\\abc.txt"
    destination_folder = "C:\\destination"
    
    FileUtils.cp(file_path, destination_folder)
    

    编辑

    正如 Tin Man 指出的那样,这是不正确的:它有效,但无论如何没有必要

    我想它应该按原样工作;它可能只是一个错字吗?正如 toro2k 所指出的,你先写了destination_folder,然后写了destination_directory

    【讨论】:

    • 我认为你应该检查你的代码,destination_directory 来自哪里?
    • 您确实必须对 Windows 路径使用反斜杠。阅读IO documentationRuby will convert pathnames between different operating system conventions if possible. For instance, on a Windows system the filename "/gumby/ruby/test.rb" will be opened as "\gumby\ruby\test.rb".
    • 实际上windows也支持“/”作为路径名分隔符,其用法与命令参数一致。所以实际上只有在涉及命令行解析时才会出现问题。
    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多