当前位置:网站首页>Open function

Open function

2022-06-24 20:48:00 m0_ sixty-two million four hundred and ninety-six thousand nine

open function : Used to open and create files

grammar :file=open( file name , Open mode , buffer )

The details of various parameters are as follows :

The file name variable is a string value that contains the name of the file you want to access .

Open mode : Determines the mode of opening files : read-only , write in , Supplemental . All available values are shown in the complete list below .

This parameter is optional , The default file access mode is read-only (r).

buffer : If buffering The value of is set to 0, There will be no deposit . If buffering Value of 1, Deposit lines when accessing files . If you will buffering The value of is set to be greater than 1 The integer of , Indicates that this is the buffer size of the cache .

If you take a negative value , The buffer size of the deposit area is the system default .

Common file opening methods :

R read-only : Open the file read-only . The pointer to the file will be placed at the beginning of the file . This is the default mode .

W write in : Open a file only for writing . Open the file if it already exists , And edit from the beginning , The original content will be deleted . If the file does not exist , Create a new file .

A Additional : Open a file for appending . If the file already exists , The file pointer will be placed at the end of the file . in other words , The new content will be written after the existing content . If the file does not exist , Create a new file to write to .

Write() Method

Write() Method to write any string to an open file , It's important to pay attention to ,python Strings can be binary data , Not just words

Write() Method does not add a newline at the end of the string (\n):

grammar :

File.write(string\n)

ad locum , The parameter passed is the content to be written to the open file

Example :

Open a file

Fo=open(“foo.txt”,w

Fo.write( Hello \n The world \n)

Close open files

Fo.close()# Close open files

Read() Method

Read() Method to read a string from an open file

grammar : Open file .read( digit )

ad locum , The parameter passed is the count of bytes to read from the opened file , This method reads in from the beginning of the file

If no digits are passed in , Will read all the contents

You can use readline() Method returns a line

grammar : file name .read() Read one row by default

file name .close()# Close open files

Example :

Here we use the above creation foo.txt file

Open a file

Fo=open(foo.txt,r+

Str=fo.read(3)# The read contents should be received with variables

Read to page 3 Bit byte

Print“ The string read is :”,str

Close open files

Fo.close()

The output of the above example :

The string read is : Hello world

example

Read a line in the file

F=open(demofile.txt,r)

Print(f.readline())

F=open

For i in rd22# Read the contents of the document by line

Print(i)

If you want to read the contents of each line one by one, you can use for loop

原网站

版权声明
本文为[m0_ sixty-two million four hundred and ninety-six thousand nine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211325594587.html