lua工具库penlight--04路径和目录

使用路径

程序不应该依赖于奇葩的系统,这样你的代码会难以阅读和移植。最糟糕的是硬编码的路径,
windows和Unix的路径分隔符正好相反。最好使用path.join,它可以帮助你解决这个问题。
pl.path提供了与Python中os.path里相同的功能。
> p = 'c:\bonzo\DOG.txt'
 > = path.normcase (p)  ---> only makes sense on Windows
c:onzodog.txt
> = path.splitext (p)
 c:onzoDOG    .txt
> = path.extension (p)
.txt
> = path.basename (p)
DOG.txt
> = path.exists(p)
false
> = path.join ('fred','alice.txt')
fredalice.txt
> = path.exists 'pretty.lua'
true
> = path.getsize 'pretty.lua'
2125
> = path.isfile 'pretty.lua'
true
> = path.isdir 'pretty.lua'
false
 
path.expanduser提供你所在系统的用户目录。
windows
> = path.expanduser '~/mydata.txt'
'C:Documents and SettingsSJDonova/mydata.txt'
 Unix
> = path.expanduser '~/mydata.txt'
/home/sdonovan/mydata.txt
 
在windows下,os.tmpname返回一个指向系统根目录中的临时文件。(通常,你无权访问根文件夹)
path.tmpname更正了这个问题,它使用TMP环境变量。
> os.tmpname()  -- not a good place to put temporary files!
's25g.'
> path.tmpname()
'C:DOCUME~1SJDonovaLOCALS~1Temps25g.1'
 
另外一个有用的函数是pl.path.package_path,可以告诉你lua模块的路径。在我的系统上,
package_path(‘pl.path’)返回C:Program FilesLua5.1lualibsplpath.lua’,
package_path(‘ifs’)则返回‘C:Program FilesLua5.1clibslfs.dll’。pl.path.package_path
使用package.searchpath实现,这个函数是lua 5.2里新增的,在lua 5.1里则由penlight提供。
 
文件操作
pl.file是一个新的模块,它提供了命名更友好的函数。例如file.read和file.write分别是utils.readfile和
utils.writefile的别名。
小的文件可以快速地读入,并只用一次操作。file.read接受传入的文件名,如果成功,以字符串返回
文件内容;否则返回nil和错误信息。如果你想以二进制读文件,还有一个可选的布尔参数(这在Unix
上相同,但对windos很重要)
在上一版的Penlight里,如果没有指定文件utils.readfile将会使用标准输入,但是这可能会引起讨厌的
bug。使用io.read '*a'可以提取标准输入。
类似地,file.write第一个参数为文件名,第二个参数为写入内容。
例如,转换文件内容到大写
require 'pl'
assert(#arg == 2, 'supply two filenames')
text = assert(file.read(arg[1]))
assert(file.write(arg[2],text:upper()))
 
复制文件尤其棘手。file.copy和file.move尝试去最好地实现。在Windows上它们使用系统CopyFile和
MoveFile函数,但是要求安装alien包(对于lua for windows,已经安装)。否则,调用系统复制命令。
如果写windows下的gui程序,则会出现一闪而过的cmd黑窗口。
 
目录操作
pl.dir提供了一些有用的函数来操作目录。fnmatch可以用来匹配符合要求的文件。getdirectories可以获取
目录下的所有子目录,getfiles则获取符合要求的文件。这些函数返回表,而不是像lfs.dir返回迭代器。
dir.makepath可以创建全路径,当然包括子目录。rmtree提供了强力的删除操作,可以删除目录下的所有文件
和子目录(类似Python里的shutils)
> = dir.makepath 't\temp\bonzo'
> = path.isdir 't\temp\bonzo'
true
> = dir.rmtree 't'
 
dir.rmtree依赖dir.walk,它提供了一个强力的扫描目录树工具。这里是它的实现:
--- remove a whole directory tree.
-- @param path A directory path
function dir.rmtree(fullpath)
     for root,dirs,files in dir.walk(fullpath) do
         for i,f in ipairs(files) do
             os.remove(path.join(root,f))
         end
         lfs.rmdir(root)
     end
end
 
dir.clonetree 可以复制目录树。第一个参数是路径,要求必须存在,第二个参数是要复制到的位置(注意,第二个
参数不能是第一个参数的子目录,否则发狂)。默认只会创建目录结构,你可以提供提供一个函数复制找到的所有
文件。
-- make a copy of my libs folder
require 'pl'
p1 = [[d:devlualibs]]
p2 = [[D:devlualibs.. ests]]
dir.clonetree(p1,p2,dir.copyfile)
一个更复制的版本,用来复制被修改过的文件。
-- p1 and p2 as before, or from arg[1] and arg[2]
dir.clonetree(p1,p2,function(f1,f2)
   local res
   local t1,t2 = path.getmtime(f1),path.getmtime(f2)
   -- f2 might not exist, so be careful about t2
   if not t2 or t1 > t2 then
     res = dir.copyfile(f1,f2)
   end
   return res -- indicates successful operation
end)
 
dir.clonetree使用path.common_prefix。如上面定义的p1和p2,共同的路径是‘d:devlua’,
‘d:devlualibs estfunc.lua’被复制到‘d:devlua est estfunc.lua’,等。
如果你要查找文件列表里共同的路径,可以用tablex.reduce来完成。
 > p3 = [[d:dev]]
> = tablex.reduce(path.common_prefix,{p1,p2,p3})
'd:dev'

原文:http://stevedonovan.github.io/Penlight/api/topics/04-paths.md.html

原文地址:https://www.cnblogs.com/xdao/p/lua_penlight04.html