File Operations, c ++ File Operations
File Operations
File Operation Procedure
The existing file is as follows: the file name is "song"
1 hiding from the rain and snow 2 hiding in the rain and snow 3 trying to forget but I won't let go 4 try to forget, but how can I leave 5 looking at a crowded street 6 and watch the bustling street 7 listening to my own heart beat 8 but I can only hear my heartbeat 9 so many people all around the world? 10 so many people in the world 11 tell me where do I find someone like you girl 12 please tell me where I can find girls like you 13 take me to your heart take me your soul 14 accompanied me with your soul 15 give me your hand before I'm old 16 to your hand, 17 show me what love is haven' t got a clue 18 ask why things are before we leave each other 19 show me that wonders can be true 20 ask miracle stage 21 they say nothing lasts forever 22 they said there is nothing we can do for a long time 23 we're only here today 24 we can also stay in line with 25 love is now or never 26 now or never go back 27 bring me far away 28 please take me away
Basic operations:
# R read-only mode
F = open ('song', 'R', encoding = 'utf-8') # open the file and assign it to the file handle f (f is actually the Memory Object of the file) data1 = f. readline () # Read a row data2 = f. read () # read all the remaining content. Do not use it when the file is large. It may not be able to read all the print (data1) print (data2)View Code
# Other operations f. tell () # view the position of the file handle pointer print (f. tell () f. seek (0) # set the current file location offset print (f. tell () print (data1) print (f. encoding) # file encoding method print (f. fileno () # File No. f. close () # close the opened file. The closed file cannot be read or written into more things.
F. flush () # refresh
F. truncate () # truncate, starting from the file handle pointer position.
# F. readlines () method will first read all rows into the memory, which is inefficient.
# Low loopfor index line in enumerate (f. readlines (): if index = 9: print ("------ I am a split line -------") print (line. strip () # Remove empty rowsView Code
Since the readlines () method is inefficient, the question is, what efficient method should we use?
# Highcount = 0for line in f: # Read rows in one row, and only one row in the memory if count = 9: print ("----- I am a split line -------") count + = 1 continue count + = 1 print (line) f. close () # at this time, f is not as a list as readlines above. You cannot print the required rows by enumeration. You can only customize a counter.View Code
# W write-only mode if the content in the written file is overwritten
F1 = open ('song2 ', 'w', encoding = 'utf-8') f1.write ('Heaven and earth is my love \ n ') f1.write ('Flowers are opening at the foot of the mountain hill \ n ')View Code
# A = append cannot be read by default
F2 = open ('song2 ', 'A', encoding = "UTF-8") # append f2.write to the file handle ("What kind of rhythm is the most swing \ n ") # f2.flush () # refreshView Code
# Read/write mode r + read/write mode and append Mode
F = open ('song2 ', 'r +') # file handle read & append print (f. readline () print (f. readline () f. write ("What kind of rhythm is the most swing \ n") f. write ("What kind of singing... \ n ") f. close ()View Code
# Write/read Mode w +
F3 = open ('song3 ', "w +", encoding = 'utf-8 ') # file handle write read f3.write ("quietly accompany you for a long time, even red eyes are not found, listen to you say you are changing now, watching me still love your smiling face this old road still not changed """) f3.seek (2) print (f3.readline () f3.close ()View Code
# Append read a +: Both append and read
F4 = open ("song3", "a +", encoding = 'utf-8 ') f4.write ("" Every time we were passing by, it was always sunny, and we thought of the past tears that started to spread at. "") f4.seek (30) print (f4.readline () f4.close ()View Code
# Binary read
F5 = open ('song', 'rb') # file handle binary file print (f5.readline () print (f5.readline () print (f5.readline () f5.close ()View Code
# Binary writing
F6 = open ('song4 ', 'wb') # file handle binary file f6.write ("Hello binary! \ N ". encode () f6.close ()View Code
# Modifying files
F = open ('png', 'R', encoding = 'utf-8') f_new = open ('png_modified', 'w', encoding = "UTF-8 ") for line in f: if "I" in line: line = line. replace ('I', 'Go') f_new.write (line) f. close () f_new.close ()View Code
With statement
To avoid forgetting to close a file after it is opened, you can use with to manage the context:
With open ('song', 'R', encoding = 'utf-8') as f: for line in f: print (line) # in this way, when the with code block is executed, file resources are automatically closed and released internally. # After Python 2.7, with supports managing the context of multiple files at the same time, for example, with open ('song1 ', 'R', encoding = 'utf-8 ') as f, open ('song2 ', 'R', encoding = 'utf-8') as f1: pass