天天看點

perl和python的讀寫檔案例子

[[email protected] python_tidb]$ cat mypy01.py
#!/usr/local/bin/python3

import glob,sys

F = open('samp_python.txt','w')
#sys.stdin = F
F.write('first line\n')
F.write('second line\n')
F.write('third line\n')
F.write('last line\n')
F.close()

F=open('samp_python.txt','r')
lines = F.readlines()
for i in (range(len(lines))) :
    print (str(i)+"::"+lines[i],end="")
[[email protected] python_tidb]$ ./mypy01.py
0::first line
1::second line
2::third line
3::last line


[[email protected] python_tidb]$ cat p.pl
#!/usr/bin/perl

# perl write file
open(F,'>','samp_perl.txt');
print F "first line\n";
print F<<EOF;
second line
third line
last line
EOF
close F;

#perl read file
open(F,'<','samp_perl.txt');
while (<F>) {
  print $.,'::',$_
}
[[email protected] python_tidb]$ ./p.pl
1::first line
2::second line
3::third line
4::last line