Changes to read and write files from Python novice learners

Source: Internet
Author: User
Tags readline

Files in addition to R, W, a mode open, but also can have a variety of combinations such as r+ w+ A + and many other ways

1, r+ Read and write mode introduction, start reading is starting from a line to read, write forever from the last write (similar to append)

#f = open ("Test.txt","r+", encoding ="Utf-8") F.readline () F.readline () F.readline ( )#no matter how it is read or seek. The file is always appended from the tail. The read cursor position is not affected when writing. Print("current cursor Position:", F.tell ()) F.write ("\ n Test r+11")Print("Insert the current cursor position for the first time:", F.tell ())Print(F.readline ())Print("current cursor Position:", F.tell ()) F.write ("\ n Test r+22")Print("second time current cursor position:", F.tell ())Print(F.readline ())Print(F.read ()) E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe E:/users/xiajinqi/pycharmprojects/twoday/file2.py Current cursor Position:51Insert the current cursor position for the first time:423Append 2 current cursor position:423second time current cursor position:435Append 3 Append 4 append 5 append 6 append 7 Append 8 append 9 append 10 Test R+Test R+Test R+Test R+Test R+Test R+Test R+Test R+Test R+Test R+11Test R+22Test R+11Test R+22Test R+11Test R+22Test R+11Test R+22Test R+11Test R+22Test R+11Test R+22Test R+11Test R+22Test R+11Test R+22

2, w+ write read (create a new file), after reading, the cursor will be at the end, read time need from Seek. And the read does not affect the writing to the location. Write at the tail.

#w+ is relatively less used. Create a new file to read and writef = open ("Test2.txt","w+", encoding='Utf-8') F.write ("1----------\ n") F.write ("2----------\ n") F.write ("3----------\ n")Print(F.readline ())Print(F.tell ()) f.seek (0)Print(F.readline ()) F.write ("test----------\ n") F.seek (0)Print(F.read ()) E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe E:/users/xiajinqi/pycharmprojects/twoday/file2.py391----------1----------2----------3----------Test----------Process finished with exit code 0


3 A + additional read. No new files are created. appended to the file.

#w+ is relatively less used. Create a new file to read and writef = open ("Test2.txt","A +", encoding='Utf-8') F.write ("1----------\ n") F.write ("2----------\ n") F.write ("3----------\ n")Print(F.readline ())Print(F.tell ()) f.seek (0)Print(F.readline ()) F.write ("test----------\ n") F.seek (0)Print(F.read ())

4, RB and WB read and write in binary mode.

#w+ is relatively less used. Create a new file to read and writef = open ("Test2.txt","RB")Print(F.readline ()) F.close ()#. Encode () Default usagef = open ("Test2.txt","WB")Print(F.write ("binnary". Encode ())) F.close () F= Open ("Test2.txt","RB")Print(F.readline ()) F.close ()


5, file modification, the contents of the file replacement.

#will not die of fleeting camphor still will be fleeting time has goneOld_file = open ("Test.txt",'R', encoding="Utf-8") New_file= Open ("Test.txt.bak","W", encoding="Utf-8") forLineinchOld_file:if "The fleeting camphor still" inchLine:line= Line.replace ("The fleeting camphor still","The time has gone and the camphor is still") New_file.write (line) Old_file.close () New_file.close () Lyrics content: Time rain spilled in the heart of you and I disappeared in the face of the feelings in the back of the time has gone camphor still summer finally went to the last number of persistent lost to the time of the tears flowed through the youth

5. Use of Python with open ("Test.txt", "R", Encoding: "Utf-8") as F

With  open ("Test.txt", ' R ', encoding= "Utf-8") as F  :    print (F.read ()) #打开多个文件with  open ("Test.txt", ' R ' , encoding= "Utf-8") as F1,    open ("Test.txt", ' R ', encoding= "Utf-8") as  F2:    print (F.read ())

6, Python implementation of Shel replacement search work

#Author:xiajinqiImportSysoperotor_type= Sys.arg[1]find_file= Sys.argv[2]find_str= Sys.argv[3]replace_str= Sys.arg[4]ifOperotor_type = ='F': With open (Find_file,'r+', encoding="Utf-8") as F: forLineinchF:ifFind_strinchLine :Print(line)Else:                Print("the found content does not exist")elifOperotor_type = ='R': With open (Find_file,'r+', encoding="Utf-8") as F1, open ("Test.bak",'w+', encoding="Utf-8") as F2: forLineinchF1:ifFind_strinchLine:line=line.replace (FIND_FILE,FIND_STR,REPLACE_STR) F2.wirte (line)Else :    Print("usage:python find.py f/r filename findstr replacestr")

Changes to read and write files from Python novice learners

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.