如何区分文件与Perl中的目录? [英] How do I distinguish a file from a directory in Perl?

查看:98
本文介绍了如何区分文件与Perl中的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遍历Perl当前目录的所有子目录,并从这些文件中获取数据。我使用grep获取给定目录中的所有文件和文件夹的列表,但是我不知道返回的值是一个文件夹名称,哪个是没有文件扩展名的文件。

I'm trying to traverse through all the subdirectories of the current directory in Perl, and get data from those files. I'm using grep to get a list of all files and folders in the given directory, but I don't know which of the values returned is a folder name and which is a file with no file extention.

如何区分?

推荐答案

您可以使用 -d 文件测试运算符来检查某物是否是目录。这里有一些常用的文件测试运算符

You can use a -d file test operator to check if something is a directory. Here's some of the commonly useful file test operators


    -e  File exists.
    -z  File has zero size (is empty).
    -s  File has nonzero size (returns size in bytes).
    -f  File is a plain file.
    -d  File is a directory.
    -l  File is a symbolic link.

请参阅 perlfunc手册页更多

此外,尝试使用 File :: Find 可以为您递归目录。这是一个寻找目录的示例....

Also, try using File::Find which can recurse directories for you. Here's a sample which looks for directories....

sub wanted {
     if (-d) { 
         print $File::Find::name." is a directory\n";
     }
}

find(\&wanted, $mydir);

这篇关于如何区分文件与Perl中的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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