How to use python files

  1. Types of
    files A file is a sequence of data stored on auxiliary storage. A
    file is a form of data storage.
    File display forms: text files and binary files

Essentially all files are stored in binary form. A text file is a file composed of a single specific code, and a binary file is directly composed of 0 and 1, without a specific code.

  1. File opening and closing
    <variable name> = open(<file name>, <open mode>) The
    path of the source file and the program in the same directory can be omitted (ie use a relative path), otherwise an absolute path is required.
file=open('meimei.txt','r')   #相对路径,只读
file=open('C:Users/lenovo/coding/meimei.txt','r')   #绝对路径,只读

Insert picture description here
Insert picture description here
(The picture comes from the Songtian python MOOC of BIT)
File closing

file=open('F:meimei.txt','r')
print(file.read())
file.close()   #文件的关闭

File content reading
There are three main ways to read file content, namely read, readline, and readlines functions. The
three methods have their own characteristics.

Operation method description
file.read(size=-1) Read in the entire content, if the parameter is given, the size before reading in
file.readline(size=-1) Read the content of a line, if the parameter is given, size length before reading the line
file.readlines(hint=-1) Read in all lines of the file, form a list with each line element. If parameters are given, read the first hint line
  • Full text reading of the file
    (read all text content at once)
file=open('F:meimei.txt','r')
print(file.read())
file.close()

We can leave the Christmas lights up 'til January
This is our place we make the rules
And there's a dazzling haze
A mysterious way about you dear
Have I known you 20 seconds or 20 years
Can I go where you go
Can we always be this close
Forever and ever
And ah take me out
And take me home
You're my my my my
Lover

(Read all text content and form a list)

file=open('F:meimei.txt','r')
print(file.readlines())
file.close()

["We can leave the Christmas lights up 'til January\n", 'This is our place we make the rules\n', "And there's a dazzling haze\n", 'A mysterious way about you dear\n', 'Have I known you 20 seconds or 20 years\n', 'Can I go where you go\n', 'Can we always be this close\n', 'Forever and ever\n', 'And ah take me out\n', 'And take me home\n', "You're my my my my\n", 'Lover']
  • Line-by-line reading of files

(Readline method)

file=open('F:meimei.txt','r')
txt=file.readline()    #分行读入,逐行处理
for txt in file:
    print(txt)
file.close()

This is our place we make the rules

And there's a dazzling haze

A mysterious way about you dear

Have I known you 20 seconds or 20 years

Can I go where you go

Can we always be this close

Forever and ever

And ah take me out

And take me home

You're my my my my

Lover

(read method)


file=open('F:meimei.txt','r')
txt=file.readlines()   #一次读入,分行处理
for line in txt:
    print(line)
file.close()

The read and readlines methods both read the entire file at once. If the file size is particularly large, it may directly damage the software, so generally small files can be read directly, and larger files can be read line by line using readline, or you can take The way of slicing.

file=open('F:meimei.txt','r')
print(file.read(100))
file.close()

We can leave the Christmas lights up 'til January
This is our place we make the rules
And there's a 

You can also use the with open method to read files, an example is as follows

path='F:meimei.txt'
with open(path,'r') as file:
    txt=file.read()
print(txt)
file.close()
  • Data file writing
path='F:meimei.txt'
file=open(path,'a')  #追加写模式
txt=file.write('I love meimei\n')
file.close()

file=open(path,'r') 
print(file.read())
file.close()

We can leave the Christmas lights up 'til January
This is our place we make the rules
And there's a dazzling haze
A mysterious way about you dear
Have I known you 20 seconds or 20 years
Can I go where you go
Can we always be this close
Forever and ever
And ah take me out
And take me home
You're my my my my
Lover
I love meimei
path='F:meimei.txt'
file=open(path,'a')    #追加写模式
lis=['meimei\n','taylor\n','swift\n']
file.writelines(lis)
file.close()

file=open(path,'r') 
print(file.read())
file.close()

We can leave the Christmas lights up 'til January
This is our place we make the rules
And there's a dazzling haze
A mysterious way about you dear
Have I known you 20 seconds or 20 years
Can I go where you go
Can we always be this close
Forever and ever
And ah take me out
And take me home
You're my my my my
Lover

meimei
taylor
swift

Guess you like

Origin blog.csdn.net/weixin_46530492/article/details/107158497