相对文件路径
我正在尝试使用类似这样的东西从几个文件中读取
IO.foreach("TeamFields.txt") { |line| fieldNames.push(line.chomp) }
它在从命令行运行时工作正常,但是当我用鞋子打包到 .exe 并运行时它找不到该文件。 有没有办法指定 .exe 的相对路径,或者我是否必须提供完整的文件路径(例如“c:\files\TeamFields.txt”)? 谢谢您的帮助。
I am trying to read in from a few files using something like this
IO.foreach("TeamFields.txt") { |line| fieldNames.push(line.chomp) }
It works fine when running the from the command line, but when I package to an .exe with shoes and run it can't find the file. Is there a way to specify a path relative the the .exe or do I have to provide the full filepath (e.g. "c:\files\TeamFields.txt")? Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为您的可执行文件未在正确的当前目录下运行。
修复当前目录(例如在快捷方式中)或修改 Ruby 程序以自动将工作目录设置为程序目录:
This is because your executable is not run with the correct current directory.
Either fix the current directory (for example in the shortcut) or modify your Ruby program to automatically set the working directory to the program directory with:
在进行相对之前,您需要正确设置“当前应用程序目录”。
用户可以使用不同的启动目录执行您的应用程序,或者系统可以使用不同的目录调用您的应用程序。
如果有问题的文件位于您的应用程序的文件夹中,您唯一需要做的就是获取该文件夹并将其设置为当前文件夹。
You need to set "Current Application Directory" correctly before going relative.
The user can execute your app with different start up dir, or system can call your app with different dir.
If files in question are in the folder of your app, the only thing you need to do is to get that folder, and set it to be current.
我不使用 ruby 进行编程,但使用 Windows 进行编程,并且相对路径很可能基于 .exe 文件的位置。
所以,是的,您最好传递文件名的完整路径。
I don't program in ruby, but I do with windows, and odds are the relative path will be based on the location of the .exe file.
So, yes, you're probably better off passing a full path for the file name.
常量
__FILE__
将包含当前正在执行的文件的完整路径。 然后,您可以使用 File 类的方法去除文件名,附加包中所需的任何其他文件的相对路径,并解析结果。The constant
__FILE__
will contain the full path to the currently executing file. You can then use methods of the File class to strip off the filename, append the relative path for whatever other file in your package it is you want and resolve the result.