def readfileOnce():
file = open('/home/zijing/readfile.txt', encoding='utf-8')
data = file.read()
file.close()
print(data,end='') #end line without '\n'
def readfileBylist():
file = open('/home/zijing/readfile.txt','r',encoding='utf-8')
for line in file.readlines():
print(line,end = '') #end line without '\n'
file.close()
def readfileBylines():
# high efficent
file = open('/home/zijing/readfile.txt','r',encoding='utf-8')
for line in file:
print(line,end = '') #end line without '\n'
file.close()
def writefile():
file = open('/home/zijing/writefile.txt','a',encoding='utf-8')
file.write("hello linux from python\n")
file.close()
readfileOnce()
readfileBylist()
readfileBylines()
writefile()
shell
#!/bin/bash
#sign: 2017/9/17 zijing
#funtion: file opreate of read and file
FILEPATH="/home/zijing/readfile.txt"
FILEWRITEPATH="/home/zijing/writefile.txt"
function readfile(){
cat $FILEPATH | while read line; do
echo $line;
done
}
function writefile(){
echo "hello linux from shell" >> $FILEWRITEPATH
}
readfile
writefile