免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1539 | 回复: 5
打印 上一主题 下一主题

perl 读目录下文件问题,请教如何略过“点”文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-26 11:00 |只看该作者 |倒序浏览
目录下只有一个文件:
[laps0@localhost ~]$ ls -a data/rawdata/profile/from_liju/hd
.  ..  20100701.TXT
代码如下:
#!/usr/bin/perl
my $path="/home/laps0/data/rawdata/profile/from_liju/hd";
opendir(DIR,$path) or die " Error : can't open $path !";
my $file;
while ($file=readdir DIR)
   {
print "the file is : $file \n";
open(FILE,"$file";
my @var = <FILE>;
print "the var is : $var[25] \n";
close(FILE);

输出信息:

[laps0@localhost wind]$ perl tmp.pl
the file is : .
the var is :  
the file is : 20100701.TXT
the var is :  
the file is : ..
the var is :  

问题是不想出来那两个点文件,请问如何操作,请费心指教,谢谢!

论坛徽章:
0
2 [报告]
发表于 2012-11-26 11:15 |只看该作者
  1. next if ($file eq "."|| $file eq "..");
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-11-26 11:16 |只看该作者
  1. #!/usr/bin/perl
  2. my $path="/home/laps0/data/rawdata/profile/from_liju/hd";
  3. opendir(DIR,$path) or die " Error : can't open $path !";
  4. my $file;
  5. while ($file=readdir DIR)
  6.    {
  7. next,if $file =~ /^\.+$/; #添加这一行就可以了
  8. print "the file is : $file \n";
  9. open(FILE,"$file";
  10. my @var = <FILE>;
  11. print "the var is : $var[25] \n";
  12. close(FILE);
复制代码

论坛徽章:
0
4 [报告]
发表于 2012-11-26 11:28 |只看该作者
多谢 :wink:,xi0ws和首天 ,绝招跟奏效!

论坛徽章:
0
5 [报告]
发表于 2012-11-26 13:16 |只看该作者
首天 发表于 2012-11-26 11:16

好吧,严格来说'.' and '..'应该是:
  1. next if $file =~ /^\.\.?$/;
复制代码

论坛徽章:
0
6 [报告]
发表于 2012-11-26 16:22 |只看该作者
kk861123 发表于 2012-11-26 13:16
好吧,严格来说'.' and '..'应该是:


多谢提醒
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP