Perl学习笔记之文件操作
作者:junjie 时间:2022-04-01 21:48:18
Perl对文件的操作,跟其它的语言类似,无非也就是打开,读与写的操作。
1. 打开文件
#! c:/perl/bin/perl -w
use utf8;
use strict;
use warnings;
my $filename = 'test.txt'; # 或者用绝对路径,如: c:/perl/Learn/test.txt
if(open(MYFILE,$filename)) # MYFILE是一个标志
{
printf "Can open this file:%s!", $filename;
close(MYFILE);
}
else{
print "Can't open this file!";
}
2. 读取文件
#! c:/perl/bin/perl -w
use utf8;
use strict;
use warnings;
my $filename = 'test.txt';
if(open(MYFILE,$filename))
{
my @myfile = <MYFILE>; #如果要读取多行,用此方法,如果只读取一行为:$myfile = <>;
my $count = 0; #要读取的行数,初始值为0
printf "I have opened this file: %s\n", $filename;
while($count < @myfile){ #遍历
print ("$myfile[$count]\n"); #注意此种写法.
$count++;
}
close(MYFILE);
}
else{
print "I can't open this file!";
}
exit;
3. 写入文件
#! c:/perl/bin/perl -w
use utf8;
use strict;
use warnings;
my $filename = 'test.txt';
if(open(MYFILE,">>".$filename)) #此种写发,添加不删除
{ #此种写法,重写文件内容 MYFILE,">".$filename
print MYFILE "Write File appending Test\n";
close(MYFILE);
}
else{
print "I can't open this file!";
}
exit;
标签:Perl,文件操作
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中的lambda表达式用法详解
2022-12-01 17:33:57
全面了解Python环境配置及项目建立
2023-11-03 18:40:44
![](https://img.aspxhome.com/file/2023/6/107876_0s.jpg)
Python使用pyyaml模块处理yaml数据
2023-11-29 10:08:39
关于Python中的编码规范
2021-11-07 02:38:02
Python ORM框架SQLAlchemy学习笔记之关系映射实例
2022-06-22 23:39:04
opencv python 图像轮廓/检测轮廓/绘制轮廓的方法
2022-08-13 06:54:59
![](https://img.aspxhome.com/file/2023/4/77864_0s.png)
Python编译成.so文件进行加密后调用的实现
2022-11-17 12:49:54
IE6局部调用PNG32合并图片
2009-03-11 21:24:00
![](https://img.aspxhome.com/file/UploadPic/20093/11/01-12s.jpg)
Python 6种基本变量操作技巧总结
2023-04-03 21:50:04
![](https://img.aspxhome.com/file/2023/2/97412_0s.jpg)
python爬虫之异常捕获及标签过滤详解
2021-08-10 07:32:06
![](https://img.aspxhome.com/file/2023/7/117977_0s.jpg)
pip 安装库比较慢的解决方法(国内镜像)
2022-09-30 04:02:29
python 统计代码耗时的几种方法分享
2023-11-03 19:51:06
![](https://img.aspxhome.com/file/2023/4/63274_0s.png)
django实现用户登陆功能详解
2021-03-04 20:19:06
![](https://img.aspxhome.com/file/2023/1/88891_0s.jpg)
理解CSS3线性渐变
2010-03-28 13:42:00
![](https://img.aspxhome.com/file/UploadPic/20103/28/01-27s.jpg)
python基础入门之普通操作与函数(三)
2023-11-01 22:52:17
![](https://img.aspxhome.com/file/2023/2/112572_0s.png)
Pycharm无法显示动态图片的解决方法
2023-01-29 23:13:42
![](https://img.aspxhome.com/file/2023/0/89420_0s.jpg)
详解python中各种文件打开模式
2023-08-30 09:10:30
python实现logistic分类算法代码
2021-12-24 14:19:15
![](https://img.aspxhome.com/file/2023/6/87806_0s.jpg)
pandas.DataFrame的for循环迭代的实现
2023-07-07 22:10:28
python 匹配url中是否存在IP地址的方法
2023-04-13 14:29:08