computer = instruction + data
(CPU execution) (stored in RAM)
The computer reads the contents of the file into memory, which is actually assigned to a variable.
File Operation Flow:
--Reading Mode
--writing method
--file_obj.read, File_obj.readline, File_obj.readlines
--file_obj.write, File_obj.writes
File_obj.close ()
Memory----->io Buffer----> Save to disk
Open File
File_obj=open (Filename,mode)
-filename:
Original string: R ' C:\temp\text.t ' #me: The path under Linux is/, so it is not treated as a special
Escape string: ' c:\\temp/test.t '
The above two are equivalent
-mode
R (), W (), A (), B ()
R+ (), w+ (), A + () append, if the file does not exist, create
Read the file
Var=file_obj.read ()
-read all read back--return string
-readline reads a line--Returns a string
-readlines read multiple lines-Returns a list of string (list)
Write file:
File_obj.write (Content_obj)
-write f.write (content_obj + ' \ n ')
-writelines
Close File
File_obj.close ()
Remove the ' \ n ' from the read-back string
Str=str.rstrip (' \ n ') rstrip--> removes the right-most
Because ReadLine---> string + ' \ n ', print comes with a return key ' \ n ', so if you don't do it, you'll make a blank line.
Reading and writing of numeric data
When writing, use STR to convert to string + ' \ n '
When reading back, use int, float to convert to numeric data.
' \ n ' is a non-printable character in ASCII
Eg1:file_obj=open ('/home/wuxy/test.txt ', ' R ')
S=fiel_obj.read ()---A string
Print S
File_obj.close ()
22nd Python File Basics