File:
A file is a virtual unit of a read-write drive provided by the operating system for a user or application
The operating core of the file is: Read, write
That is, we only need to read and write operations, that is, the operating system initiated the request, and then by the operating system to the user or application
Read and write to the file into specific hard disk instructions (such as control disc rotation, control the mechanical arm movement to read and write data)
Because memory is not able to persist data permanently, we need to save the data to the hard drive whenever we want to.
And the operation of the file can be implemented on the hardware
# Read the data from the hard drive into memory
#rawstring
F=open (R ' E:\SH_fullstack_s2\day08\a.txt ', mode= ' R ') # initiates a request to the operating system, requires the operating system to open the file, consumes the operating system's resources
Data=f.read () # Opens an originating read request to the operating system, which is transferred by the operating system to a specific hard disk operation and reads the contents from the hard disk into memory
Print (data)
F.close () #向操作系统发起请求, requires the operating system to shut down files, recycle system resources
Summary
#1, open File
#2, read and write
#3, close
#1, Absolute path: Start writing a full path from the drive letter (c:\\ e:\\ d:\\)
#2, relative path: Start looking for the folder where the current execution file is located
F=open (R ' A\a.txt ')
# There are two ways to control the contents of a read-write file: T-mode text,b Model bytes
#注意:
Both #1, T, and B cannot be used alone and need to be in use with one of the r/w/a
#2, the default content format is T
#3, only text files can be used in T mode, and only text files have the concept of character encoding
# There are three basic modes of manipulating files:
#1, R, default
#2, W
#3, a
#r: Read, readonly mode
#1, read only, can't write
#2, when the file does not exist, will be error, when the file exists, the file pointer will be moved to the beginning
# F=open (R ' b.txt ', mode= ' RT ', encoding= ' utf-8 ')
# Data1=f.read ()
# print (' First time: ', data1)
# # Print (Type (data1))
#
# # Data2=f.read ()
# # Print (' Second time: ', data2)
# F.close ()
# F=open (R ' b.txt ', mode= ' RT ', encoding= ' utf-8 ')
# Data2=f.read ()
# print (' Second time: ', data2)
# F.close ()
# F=open (R ' b.txt ', mode= ' RT ', encoding= ' utf-8 ')
# # Print (f.readable ())
# # Print (F.readline (), end= ")
# # Print (F.readline (), end= ")
#
# Lines=f.readlines ()
# Print (lines)
# F.close ()
#
#w: Write-only mode
#1, can only write, cannot read
#2, an empty file is created when the file does not exist, and the contents of the file are emptied when the file exists
# F=open (R ' c.txt ', mode= ' wt ', encoding= ' utf-8 ')
# # Print (f.readable ())
# # F.read ()
# # # F.write (' Hello my god, hello, my land! Ah ah ah ah \ n ')
# # F.write (' 4444\n ')
# # # F.write (' 5555 ')
#
# lines=[' 1111\n ', ' 2222\n ', ' 33333\n '
# # for line in lines:
# # F.write (line)
# F.writelines (lines)
#
# F.close ()
#a: Append write mode only
#1, can only write, cannot read
#2, creates an empty file when the file does not exist, moves the pointer to the end of the file when the file exists
# f=open (' D.txt ', mode= ' at ', encoding= ' utf-8 ')
# Print (f.readable ())
# F.read ()
# f.write (' 555555\n6666\n ')
# f.writelines ([' 77777\n ', ' 8888888\n ', ' 999999999\n '])
# F.close ()
#b: Binary mode
#注意:
#1, must not specify character encoding, only the T mode is related to character encoding
#2, B is a binary mode, is a common file read mode, because all the files in the hard disk are stored in binary form
# f=open (' egon.jpg ', ' RB ')
# Data=f.read ()
# Print (Type (data))
# F.close ()
# f=open (' Oldsiji.mp4 ', ' RB ')
# Data=f.read ()
# F.close ()
# F=open (R ' F:\oldsiji.mp4 ', ' WB ')
# f.write (data)
# F.close ()
# cyclic Reading
# f=open (' d.txt ', ' RT ', encoding= ' utf-8 ')
# for line in F:
# Print (line)
# F.close ()
# f=open (' D.txt ', ' RB ')
# for line in F:
# Print (line)
# F.close ()
# f=open (' Oldsiji.mp4 ', ' RB ')
# for line in F:
# Print (line)
# F.close ()
# f=open (' E.txt ', ' WB ')
# f.write (' Hello ', encode (' Utf-8 '))
#
# F.close ()
# f=open (' e.txt ', ' AB ')
# f.write (' Hello, I'm Egon '. Encode (' Utf-8 '))
# F.close ()
# Context Management
# with open (' e.txt ', ' RB ') as F,open (' j.txt ', ' wt ', encoding= ' utf-8 ') as F1:
# #文件的操作
# Src_data=f.read ()
# res=src_data.decode (' Utf-8 ')
# # Print (Res,type (res))
# F1.write (RES)
With open (' e.txt ', ' RB ') as F,\
Open (' J.txt ', ' WB ') as F1:
#文件的操作
F1.write (F.read ())
Python file processing