发布:thebaby 来源:net 【大 中 小】

perl 文件操作,包括打开、关闭文件,读取、定入文件等。
原文链接:http://www.jbxue.com/article/3153.html

打开、关闭文件
open (filevar, filename)
filevar为文件句柄,或者说是程序中用来代表某文件的代号
filename为文件名,其路径可为相对路径,亦可为绝对路径

open(FILE1,"file1");
open(FILE1, "/u/jqpublic/file1");

打开文件时必须决定访问模式

open(FILE1,"file1");
read
open(outfile,">outfile");

write写模式将原文件覆盖,原有内容丢失

open(appendfile, ">>appendfile");
append

open的返回值用来确定打开文件的操作是否成功,成功时返回非零值,失败时返回零:

if (! open(MYFILE, "myfile")) {
die ("cannot open input file file1\n");
}
open (MYFILE, "file1") || die ("Could not open file"); close (MYFILE);

例子.读文件并显示

#!/usr/bin/perl

&gotest("/home/macg/perltest/gogo");
&gotest("/home/macg/www/index.html");
&gotest("jk"); sub gotest{
my(@tmp)=@_;
open (MYFILE, $tmp[]) || die ("Could not open file");
@array = <MYFILE>; 此句不是读一行,而是读整个文件
foreach (@array) {
print $_;
}
close(MYFILE);
}
[macg@localhost perltest]$ ./tip.pl

kkkkk 第一个文件gogo读出

第二个文件index.html读出
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<frameset rows=20%,*>
  <frame src="title.html" frameborder="no" scrolling="no">
  <frameset cols=30%,*>
    <frame src="index-left.htm" frameborder="no" name="left">
    <frame src="main-right.html" frameborder="no" name="right">
  </frameset>
</frameset>

第三个文件jk不存在,程序走die语句
Could not open file at ./tip.pl line 9

打开管道文件--------操作非常简单,就是以带管道符号的命令作为文件名字符串
执行一个管道命令
假设管道命令创建一个临时文件
再OPEN这个临时文件到句柄

[macg@localhost perltest]$ vi tip.pl
#!/usr/bin/perl &gotest("ls -l |"); sub gotest{
my(@tmp)=@_;
open (MYFILE, $tmp[]) || die ("Could not open file");
@array = <MYFILE>;
foreach (@array) {
print $_;
}
close(MYFILE);
}
[macg@localhost perltest]$ ./tip.pl
total
-rw-rw-r-- macg macg Mar : gogo
-rwxrwxr-x macg macg Mar : tip.pl

读文件

$line = <MYFILE>;
读一行
并把文件指针向后移动一行
@array = <MYFILE>;
读全部
文件的每一行(含回车符)为@array的一个字符串元素
最简单的显示文件
@array = <MYFILE>; 一次读整个文件,读入一个字符串数组

foreach (@array) { 再打印字符串数组的每一个元素(每一行)
print $_;
}

[macg@localhost perltest]$ ./tip.pl
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <frameset rows=%,*>
  <frame src="title.html" frameborder="no" scrolling="no">
  <frameset cols=%,*>
  </frameset>
</frameset>
my($line);
while ($line=<MYFILE>) {循环读一行,读不出就为NULL()
print $line;
} $line =<STDIN> ; 从键盘读一行,类似C的gets();

chomp 函数,截去变量尾端的\n换行,常与键盘输入合用,方法有二種:

1)$yourans=<STDIN>;
chomp $yourans;

2)chomp ($yourans=<STDIN>);

注意:一定不要用while (chomp($line=<MYFILE>)),因为chomp总是返回0值,和while($line=<MYFILE>)是不同的
while($line=<MYFILE>){
chomp($line); 把 chomp放在循环里边

}

<> 和 <STDIN> 的区别
先说相同点:都支持标准输入读取
不同点:<> 可以将输入定向到命令行参数
vi readfile.pl

#! /usr/bin/perl
while (<>) {
print;
}

./readfile.pl index.html 就是读取第一个命令行参数所指的文件
./readfile.pl 如果不带任何参数执行,就是由标准输入STDIN读取

有关perl 文件操作总结的内容,读到这里,是不是有种越种越清晰的感觉了呢?!
Let's go!

写文件 print/printf 句柄 (字串);
open(OUTFILE, ">outfile");
print OUTFILE ("Here is an output line.\n");
print STDERR ("File file1 exists.\n");
print STDOUT ("File file1 exists.\n");

最简单的文件COPY

#!/usr/bin/perl
&gotest("ls -l |","test");

sub gotest{
my(@tmp)=@_;
open (READFILE, $tmp[0]) || die ("Could not open file");
open (WRITEFILE, ">".$tmp[1]) || die ("Could not open file");
my($line);
while ($line=<READFILE>) {
print WRITEFILE $line;
}
close(READFILE);
close(WRITEFILE);
}

[macg@localhost perltest]$ ./tip.pl
[macg@localhost perltest]$ ls
gogo test tip.pl
[macg@localhost perltest]$ cat test
-rw-rw-r-- 1 macg macg 6 Mar 16 13:06 gogo
-rwxrwxr-x 1 macg macg 297 Mar 17 17:43 tip.pl
上例同时也是一次管道文件的建立,相当于ls –l >test

-e 文件是否存在 -d 目录是否存在

#!/usr/bin/perl

chomp($file=<>);
&gotest($file);

sub gotest{
my(@tmp)=@_;
if (-e $tmp[0]) {
print "file is exist\n";
} else { print "file not found\n"; }
}

[macg@localhost perltest]$ ./tip.pl
gogo
file is exist
[macg@localhost perltest]$ ./tip.pl
kd
file not found

#!/usr/bin/perl

chomp($file=<>);
&gotest($file);

sub gotest{
my(@tmp)=@_;
if (-d $tmp[0]) {
print "directory is exist\n";
} else { print "directory not found\n"; }
}

[macg@localhost perltest]$ ls -F
gogo test testdir/ tip.pl*

[macg@localhost perltest]$ ./tip.pl
kj
directory not found
[macg@localhost perltest]$ ./tip.pl
testdir
directory is exist

if (!-e $file) 如果文件不存在
-r,-w,-x 权限
if (-w $file) {
print "$file 写权限!\n";
}

if (-x $file) {
print "$file 读权限!\n";
}

-z是否为空文件,-s是否非空
if (-z $tmp[0]) {
print "file is empty\n";
}
if ($len= -s $tmp[0]) { -s不仅能判断文件非空,还兼有计算文件大小的工作
print "file length is $len \n";
}
[macg@localhost perltest]$ touch pp
[macg@localhost perltest]$ ./tip.pl
pp
file is empty

[macg@localhost perltest]$ ./tip.pl
gogo
file length is 6

-l 是否为符号链接
-T 是否为文本文件

基本文件操作
删文件

#!/usr/bin/perl

chomp($file=<>);
&gotest($file);

sub gotest{
my(@tmp)=@_;
my($len);
unlink $tmp[0] if -e $tmp[0];
}

[macg@localhost perltest]$ ls
go test testdir tip.pl

[macg@localhost perltest]$ ./tip.pl
go

[macg@localhost perltest]$ ls
test testdir tip.pl
[macg@localhost perltest]$ ls
dd test testdir tip.pl

[macg@localhost perltest]$ ./tip.pl
/home/macg/perltest/dd 全路径删除

[macg@localhost perltest]$ ls
test testdir tip.pl

rename("原文件名", "新名");

#!/usr/bin/perl
&gotest("gogo","dd");

sub gotest{
my(@tmp)=@_;
rename($tmp[0],$tmp[1]);
}

[macg@localhost perltest]$ ls
gogo pp test testdir tip.pl

[macg@localhost perltest]$ ./tip.pl

[macg@localhost perltest]$ ls
dd pp test testdir tip.pl

取文件属性,共13个属性

#!/usr/bin/perl

chomp($file=<>);
&gotest($file);

sub gotest{
my(@tmp)=@_;
my(@sta)=stat($tmp[0]);
my($j);
for($j=0;$j<13;$j++) {
print "no.$j is $sta[$j] \n";
}
}

[macg@localhost perltest]$ ls
test testdir tip.pl
[macg@localhost perltest]$ ./tip.pl
test
no.0 is 770
no.1 is 809748
no.2 is 33204
no.3 is 1
no.4 is 500 uid
no.5 is 500
no.6 is 0
no.7 is 103 length文件大小
no.8 is 1174127246
no.9 is 1174124625
no.10 is 1174124625
no.11 is 4096
no.12 is 16

文件copy命令 必须先use模块File

#!/usr/bin/perl

chomp($file=<>);
chomp($file2=<>);
&gotest($file,$file2);

sub gotest{
my(@tmp)=@_;
use File::Copy; 在perl主目录下查找File/Copy.pm

copy($tmp[0], $tmp[1]);
}

[macg@localhost perltest]$ ./tip.pl
test
newtest
[macg@localhost perltest]$ ls
newtest test testdir tip.pl

[root@localhost perltest]# ls -F /usr/lib/perl5/5.8.6/File
Basename.pm CheckTree.pm Compare.pm Copy.pm DosGlob.pm Find.pm Path.pm Spec/ Spec.pm stat.pm Temp.pm

目录操作
chdir("testdir") || die "$!";
mkdir($dir, 0755) || die "$!";
rmdir("testdir") || die "$!";

#!/usr/bin/perl

chomp($directory=<>);
chomp($choice=<>);
&gotest($directory,$choice);

sub gotest{
my(@tmp)=@_;

if($tmp[1]) {
mkdir($tmp[0], 0755) || die "$!";
} else {
rmdir($tmp[0]) || die "$!";
}
}

[macg@localhost perltest]$ ./tip.pl
newdir
1
[macg@localhost perltest]$ ls -F
newdir/ newtest test testdir/ tip.pl*
[macg@localhost perltest]$ ./tip.pl
testdir
0
Directory not empty at ./tip.pl line 13, <> line 2
rmdir的die信息

改变文件属性和所属 (需在root下才能起作用。换句话说,这是必须在ROOT下执行的PERL语句)
chmod(0755, "file1.txt", "file2.txt");
$uid=500;
$gid=500;
chown($uid, $gid, "file1.txt", "file2.txt");

本文原始链接:http://www.jbxue.com/article/3153.html

分享:perl 文件操作总结的更多相关文章

  1. perl文件操作

    Perl 文件操作 Perl 使用一种叫做文件句柄类型的变量来操作文件. 从文件读取或者写入数据需要使用文件句柄. 文件句柄(file handle)是一个I/O连接的名称. Perl提供了三种文件句 ...

  2. [Perl][文件操作]判断文件是否为符号链接(Unicode路径)

    Win32API::File 判断文件/文件夹是否为符号链接 Win32::Unicode 好像无法做这方面的判断,只能判断是否为目录.文件.文件是否存在. Win32API::File 则支持 Ge ...

  3. 16-Perl 文件操作

    1.Perl 文件操作Perl 使用一种叫做文件句柄类型的变量来操作文件.从文件读取或者写入数据到文件需要使用文件句柄.文件句柄(file handle)是一个I/O连接的名称.Perl提供了三种文件 ...

  4. python 学习分享-文件操作篇

    文件操作 f_open=open('*.txt','r')#以只读的方式(r)打开*.txt文件(需要与py文件在同一目录下,如果不同目录,需写全路径) f_open.close()#关闭文件 打开文 ...

  5. 使用 Python 进行稳定可靠的文件操作

    程序需要更新文件.虽然大部分程序员知道在执行I/O的时候会发生不可预期的事情,但是我经常看到一些异常幼稚的代码.在本文中,我想要分享一些如何在Python代码中改善I/O可靠性的见解. 考虑下述Pyt ...

  6. Java最全文件操作实例汇总

    本文实例汇总了Java文件操作.分享给大家供大家参考,具体如下: 1.创建文件夹 ? 1 2 3 4 5 6 7 8 9 10 11 //import java.io.*; File myFolder ...

  7. python成长之路第三篇(4)_作用域,递归,模块,内置模块(os,ConfigParser,hashlib),with文件操作

    打个广告欢迎加入linux,python资源分享群群号:478616847 目录: 1.作用域 2.递归 3.模块介绍 4.内置模块-OS 5.内置模块-ConfigParser 6.内置模块-has ...

  8. iOS学习之iOS沙盒(sandbox)机制和文件操作(一)

    1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...

  9. delphi关于文件操作集锦

        关于文件操作集锦 取得该快捷方式的指向EXE关键词:快捷方式 LNK unit Unit1; interface usesWindows, Messages, SysUtils, Varian ...

随机推荐

  1. Java线程同步的方式

     java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查),      将会导致数据不准确,相互之间产生冲突,因此加入同步锁以避免在该线程没有完成操作之前,被其他线程的 ...

  2. Java学习笔记——单例设计模式Singleton

    单例设计模式:singleton 解决的问题: 确保程序在运行过程中,某个类的实例instance只有一份. 特点: 1 构造函数私有化 2 自己内部声明自己 3 提供一个public方法,负责实例化 ...

  3. 重构2-Move Method(方法移动)

    重构同样非常简单,以至于人们并不认为这是一个有价值的重构.迁移方法(Move Method),顾名思义就是将方法迁移到合适的位置.在开始重构前,我们先看看一下代码: ) ) return 0.03; ...

  4. chrom浏览器避免弹出“确定要离开此面吗?”提示框

    一.避免弹出提示框 在网上搜了很多,答案大都是设置window.onbeforeunload=null ,但是试用之后无效. 这个问题放了两天之后返回来再次想,终于找到了答案,在此和大家分享一下: 解 ...

  5. 关于ADO.NET 实体数据数据模型无法为Mysql 选择6.0 解决方案

    错误:您的项目引用了最新实体框架:但是,找不到数据链接所需的与版本兼容的实体框架数据库....... 图片:

  6. SVN管理规范

    命名规范 tags 正式版 REL-X.X.X branches 发版前 RB-X.X.X 新功能 TRY-XXX 修BUG BUG-XXXX trunk 开发 使用注意事项 负责而谨慎地提交自己的代 ...

  7. 【转载】TalkingData首席金融行业专家鲍忠铁:18亿数据解读移动互联网

    http://www.36dsj.com/archives/33417 鲍忠铁:大家下午好! 今天我会讲三个议题,一是用18亿数据解读现在移动互联网的生态圈.二是看看数据有什么样的应用.三是大数据的隐 ...

  8. 跟我一起学习ASP.NET 4.5 MVC4.0(五)(转)

    前面几篇文章介绍了一下ASP.NET MVC中的一些基础,今天我们一起来学习一下在ASP.NET MVC中控件的封装.在页面中我们会经常使用到Html对象,来程序控件,当然这里的控件不是说ASP.NE ...

  9. 浅谈sql 、linq、lambda 查询语句的区别

    浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...

  10. JavaScript语言基础-环境搭建

    我们要想编写和运行JavaScript脚本,则需要:JavaScript编辑工具和JavaScript运行测试环境.下面我们分别介绍一下.JavaScript编辑工具JavaScript编辑工具最简单 ...