python-文件操作seek(单个字符替换)

时间:2022-05-13 08:56:58

1、seek简单写入字符

root@kali:~/python# ls
contact_list.txt      fileinputbak.py       oldtext.txt      tab.pyc       xwbtest.txt
contact_list.txt.bak  fileinputreadfile.py  scan1.py         test.py
csvt01                manage_query.py       scanhostport.py  test.txt
csvtpy                newtext.txt           tab.py           userinput.py
root@kali:~/python# touch fileseek.py
root@kali:~/python# ls
contact_list.txt      fileinputbak.py       newtext.txt      tab.py    userinput.py
contact_list.txt.bak  fileinputreadfile.py  oldtext.txt      tab.pyc   xwbtest.txt
csvt01                fileseek.py           scan1.py         test.py
csvtpy                manage_query.py       scanhostport.py  test.txt
root@kali:~/python# vim fileseek.py 
root@kali:~/python# cat fileseek.py 
#!/usr/bin/python
# --*-- coding:utf-8 --*--


#f = open('contact_list.txt','r+') //相当于with open()文件操作
with open('contact_list.txt','r+') as f: //'r+'为文件可以写,并成f=open()

old = f.read() //读取整个文件到old里面

f.seek(14) //意思是从文件开头开始数到14个字符,并从14个字符开始写入new_line\n

f.write('new_line\n')

root@kali:~/python# cat contact_list.txt
thomas it m 18912378667 shanghai


jack op m 15876544332 beijin


lilei it m 15325635588 *


flake it w 18900378667 nanchang


alex op m 15833544332 shijiazang


lili lp m 15123635588 hangzhou


mayun xi m 18912345667 ningbo


zhang py w 15876541222 yunnan


lisi op m 15333333333 haikou


wangwu java m 18999999999 shandong


song js w 15000000000 shenzhen


caiqi js w 15555555555 huizhou



root@kali:~/python# vim fileseek.py 
root@kali:~/python# python fileseek.py 
root@kali:~/python# cat contact_list.txt
thomas it m 18new_line //意思是从文件开头开始数到14个字符,并从14个字符开始写入new_line\n
shanghai


jack op m 15876544332 beijin


lilei it m 15325635588 *


flake it w 18900378667 nanchang


alex op m 15833544332 shijiazang


lili lp m 15123635588 hangzhou


mayun xi m 18912345667 ningbo


zhang py w 15876541222 yunnan


lisi op m 15333333333 haikou


wangwu java m 18999999999 shandong


song js w 15000000000 shenzhen


caiqi js w 15555555555 huizhou


root@kali:~/python# 


2、seek,文件开头写入字符

root@kali:~/python# 
root@kali:~/python# cat contact_list.txt
thomas itm 18new_line
shanghai


jack op m 15876544332 beijin


lilei it m 15325635588 *


flake it w 18900378667 nanchang


alex op m 15833544332 shijiazang


lili lp m 15123635588 hangzhou


mayun xi m 18912345667 ningbo


zhang py w 15876541222 yunnan


lisi op m 15333333333 haikou


wangwu java m 18999999999 shandong


song js w 15000000000 shenzhen


caiqi js w 15555555555 huizhou


root@kali:~/python# vim fileseekstart.py
root@kali:~/python# cat fileseekstart.py 
#!/usr/bin/python
# --*-- coding:utf-8 --*--


#f = open('contact_list.txt','r+') #//相当于with open()文件操作
with open('contact_list.txt','r+') as f: #'r+'为文件可以写,并成f=open()

old = f.read()#读取整个文件到old里面

f.seek(0) #意思是从文件开头开始数到0个字符,并从文件开头写入paul

f.write('paul')

root@kali:~/python# python fileseekstart.py 
root@kali:~/python# cat contact_list.txt
paulas itm 18new_line
shanghai


jack op m 15876544332 beijin


lilei it m 15325635588 *


flake it w 18900378667 nanchang


alex op m 15833544332 shijiazang


lili lp m 15123635588 hangzhou


mayun xi m 18912345667 ningbo


zhang py w 15876541222 yunnan


lisi op m 15333333333 haikou


wangwu java m 18999999999 shandong


song js w 15000000000 shenzhen


caiqi js w 15555555555 huizhou


root@kali:~/python#