led 网站建设,龙岩做网站开发哪家做的好,canvas做的网站,咨询公司的成本费用有哪些perl读取目录#xff0c;写入文件
此脚本有两个输入参数#xff0c;第一个参数为需要打印的文件目录#xff0c;第二个参数为打印后的文件名#xff1b;
该脚本名称为out_file_full_path
#!/bin/perluse 5.010;
my $dir $ARGV[0]; # 此为第一个参数#xff1b;
opendi…perl读取目录写入文件
此脚本有两个输入参数第一个参数为需要打印的文件目录第二个参数为打印后的文件名
该脚本名称为out_file_full_path
#!/bin/perluse 5.010;
my $dir $ARGV[0]; # 此为第一个参数
opendir my $dh, $dir or die Cannot open $dir: $!;my $out_file $ARGV[1]; # 此为第二个参数
open OUT, $out_file or die Cannot open $out_file:$!;foreach $file (readdir $dh) {next if $file eq . or $file eq ..; # 将当前目录.和上层目录..排除在打印列表之外my $full_dir $dir/$file; #加上目录路径若不需打印目录则注释改行修改下一行的$full_dir为$file即可print OUT $full_dir . \n; #将文件和目录写入OUT文件中每行添加一个\n用于换行print One file in $dir is $file\n; #此行为debug 调试打印可注释
}closedir $dh; # 关闭打开文件夹的句柄
close OUT; #关闭打开文件的句柄举例说明
# 现在在一个名为test的文件夹test里面有3个文件分别为test1.v,test2.v,test3.v
# 在终端中输入上述命令
[xxxlocal]$ ./out_file_full_path test test.out# 返回结果有两个第一个即为打印在终端的
One file in test is test1.v
One file in test is test2.v
One file in test is test3.v
# 第二个为输出的文件名称为test.out
# 在终端中使用cat命令获取文件内容
cat test.out
# 返回值为
$PATH/test/test1.v
$PATH/test/test2.v
$PATH/test/test3.v
# 其中$PATH为test所在路径