相对文件路径

发布于 2024-07-17 17:16:34 字数 252 浏览 6 评论 0原文

我正在尝试使用类似这样的东西从几个文件中读取

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 技术交流群。

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

发布评论

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

评论(4

厌倦 2024-07-24 17:16:34

这是因为您的可执行文件未在正确的当前目录下运行。

修复当前目录(例如在快捷方式中)或修改 Ruby 程序以自动将工作目录设置为程序目录:

Dir.chdir(File.dirname($PROGRAM_NAME))

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:

Dir.chdir(File.dirname($PROGRAM_NAME))
末が日狂欢 2024-07-24 17:16:34

在进行相对之前,您需要正确设置“当前应用程序目录”。
用户可以使用不同的启动目录执行您的应用程序,或者系统可以使用不同的目录调用您的应用程序。

如果有问题的文件位于您的应用程序的文件夹中,您唯一需要做的就是获取该文件夹并将其设置为当前文件夹。

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.

陪我终i 2024-07-24 17:16:34

我不使用 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.

无畏 2024-07-24 17:16:34

常量__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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文