当前位置:网站首页>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
边栏推荐
- Hongxiang Yunteng is compatible with dragon lizard operating system, and the product runs stably
- Predicate
- What will you do if you have been ignored by your leaders at work?
- Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI
- Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?
- 顺序栈1.0版本
- 苹果、微软、谷歌不再掐架,今年要合力干一件大事
- CVPR 2022缅怀孙剑!同济、阿里获最佳学生论文奖,何恺明入围
- Use the transparent [x] cross button image in the dialog
- Two fellow countrymen from Hunan have jointly launched a 10 billion yuan IPO
猜你喜欢

Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable

Agency mode -- Jiangnan leather shoes factory

The Network Security Review Office launched a network security review on HowNet, saying that it "has a large amount of important data and sensitive information"

《梦华录》“超点”,鹅被骂冤吗?

传统的IO存在什么问题?为什么引入零拷贝的?

Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

对“宁王”边卖边买,高瓴资本“高抛低吸”已套现数十亿

二叉树的基本性质与遍历

Basic properties and ergodicity of binary tree
随机推荐
Basic concepts and definitions of Graphs
海泰前沿技术|隐私计算技术在医疗数据保护中的应用
It is said that Tencent officially announced the establishment of "XR" department to bet on yuanuniverse; Former CEO of Google: the United States is about to lose the chip competition. We should let T
C语言实现扫雷(简易版)
实现基于Socket自定义的redis简单客户端
Open programmable infrastructure (OPI) project, redefining dpu/ipu
微信小程序中使用vant组件
京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
字节、腾讯也下场,这门「月赚3000万」的生意有多香?
Bridging mode -- law firm
Basic properties and ergodicity of binary tree
Cooking business experience of young people: bloggers are busy selling classes and bringing goods, and the organization earns millions a month
微信小程序自定义tabBar
Predicate
传统的IO存在什么问题?为什么引入零拷贝的?
Map跟object 的区别
"Ningwang" was sold and bought at the same time, and Hillhouse capital has cashed in billions by "selling high and absorbing low"
伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI
图的基本概念以及相关定义
[performance tuning basics] performance tuning strategy