- cat /dev/null > ./null.txt 清空文件内容
shell 按行读取文件 ,不过for 是按字符串读取,行中无空格等与while相同读取整行
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
方法1
while read line
do
echo $line
done<$1
方法2
cat $1 |while read line
do
echo $line
done
方法3
for line in `cat $1`
do
echo $line
done
read line 好像readline() 快一点
with open(fr) as frr:
for line in frr:
print(line)
real 0m30.935s
user 0m0.523s
sys 0m0.385s
----------------------
with open(fr) as frr:
7 for line in frr:
8 line = frr.readline()
9 print(line)
real 0m14.752s
user 0m0.306s
sys 0m0.210s
______________
with open(fr) as frr:
7 for line in frr.readlines():
8 print(line)
real 0m31.145s
user 0m0.544s
sys 0m0.415s
逐行读取文件,将每行中以分隔符分割的最后一列赋值给变量a,变量a可能会含有换行符,或者在原文件中最后一列后增加一列,读取倒数第二列时就不包含换行符了。
网友评论