检查目录是否存在于lua中? [英] Check if directory exists in lua?

查看:211
本文介绍了检查目录是否存在于lua中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查lua中是否存在一个目录,最好不要使用LuaFileSystem模块?

  os.path.isdir(路径)

$ b $问题是,股票Lua分布(几乎)只包括在标准C中指定的功能。标准C没有假设实际上是一个文件(或甚至操作系统),所以 os io 模块不提供标准C库中不可用的访问信息。



如果您尝试使用纯C标准进行编码,您将会遇到同样的问题。 p>

有一个机会,您可以通过尝试使用它来了解文件夹是否存在。如果您希望它存在并且对您可写,则在那里创建一个临时文件,如果成功,文件夹就存在。如果失败,当然可能无法区分不存在的文件夹和权限不足。



到目前为止获得特定答案的最轻的答案对于那些提供你需要的信息的操作系统特定的函数调用来说将是一个精简的绑定。如果您可以接受 lua alien 模块,那么您可以使用纯粹的Lua来进行绑定。



更简单但稍重的是接受Lua文件系统。它提供了一个可移植的模块,支持大多数人想了解的文件和文件系统。


How do I check if a directory exists in lua, preferably without using the LuaFileSystem module if possible?

Trying to do something like this python line:

os.path.isdir(path)

解决方案

The problem is that the stock Lua distribution (nearly) only includes features that are specified in standard C. Standard C makes no presumptions about there actually being a file system of any specific sort out there (or even an operating system, for that matter), so the os and io modules don't provide access information not available from the standard C library.

If you were attempting to code in pure standard C, you would have the same issue.

There is a chance that you can learn whether the folder exists implicitly from an attempt to use it. If you expect it to exist and be writable to you, then create a temporary file there and if the that succeeds, the folder exists. If it fails, you might not be able to distinguish a non-existent folder from insufficient permissions, of course.

By far the lightest-weight answer to getting a specific answer would be a thin binding to just those OS-specific function calls that provide the information you need. If you can accept the lua alien module, then you can like do the binding in otherwise pure Lua.

Simpler, but slightly heavier, is to accept Lua File System. It provides a portable module that supports most things one might want to learn about files and the file system.

这篇关于检查目录是否存在于lua中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆