为什么我不能打开Perl的readdir返回的文件? [英] Why can't I open files returned by Perl's readdir?

查看:83
本文介绍了为什么我不能打开Perl的readdir返回的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我知道这是另一个新手问题,但我非常沮丧,希望再次受到启发.在大家的指导下,我已经学习了如何使用glob函数读取目录中每个文件的内容.现在,我尝试使用readdir-foreach组合执行相同的操作,但我不断收到无法打开文件:权限被拒绝"错误.为什么在与管理员相同的目录,相同的文件和相同的情况下发生这种情况.有人可以告诉我我在做什么错吗?谢谢.

Well, I know this is another newbie question but I'm very frustrated and I'm looking to be enlightened again. With the guidance of you guys, I've already learnt how to use the glob function to read the contents of each file in a directory. Now I'm trying the readdir-foreach combination to do the same thing but I keep receiving "Cannot open file: Permission denied" error. Why is this happening with the same directory , the same files and the same me as Administrator. Can someone kindly show me what I'm doing wrong? Thanks.

以下代码使用glob函数,并且可以正常工作:

The following code uses the glob function and it works:

#! perl
my $dir = 'f:/corpus/';
my @files = glob "$dir/*";
foreach my $file (@files) {
open   my $data, '<',"$file" or die "Cannot open FILE";
while(<$data>) {
...}

以下代码失败,错误消息显示无法打开FILE:权限被拒绝".但为什么?

The following code fails and the error message says "Cannot open FILE: Permission denied". But why?

#! perl
my $dir = 'f:/corpus/';
opendir (DIR,'f:/corpus/') or die "Cannot open directory:$!";
my @files=readdir(DIR);
closedir DIR;
foreach my $file (@files) {
open   my $data, '<',"$file" or die "Cannot open FILE:$!";
while(<$data>) {
...}

推荐答案

readdir()函数仅返回文件名,而不返回完整路径.因此,您正在尝试打开例如"foo.txt"而不是"f:\ corpus \ foo.txt".

The readdir() function returns only the file's name, not a full path. So you are trying to open e.g. "foo.txt" instead of "f:\corpus\foo.txt".

这篇关于为什么我不能打开Perl的readdir返回的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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