当前位置:网站首页>File and directory operations (5)
File and directory operations (5)
2022-07-28 15:37:00 【WHJ226】
Catalog
1. establish , Open and write files
1. establish , Open and write files
To open a file, you usually use with sentence .
After opening the file , Close it in time . If you forget to close , May bring unexpected problems . To avoid problems , Need to use with Statement instead of open() Method .
The general syntax format is as follows :
with open(filename, mode='r') as f:
f.write(string)| Parameter values | explain | remarks |
| r | Open the file in read-only mode . | The file must exist . |
| rb | Open the file in binary format , And in read-only mode . Such as images 、 Voice etc. . | |
| r+ | After opening the file , You can read the contents of the file and write new contents and overwrite the original contents . | |
| rb+ | Open the file in binary format , And adopt read-write mode . | |
| w | Open the file in read-only mode . | If the file exists , Then cover it ; otherwise , Create a new file . |
| wb | Open the file in binary format , And in read-only mode . Such as images 、 Voice etc. . | |
| w+ | After opening the file , Empty the original content first , Make it an empty file , Have read and write access to this empty file . | |
| wb+ | Open the file in binary format , And use read-write mode . |
for example :
string = ' open douban file '
with open('douban.txt', mode='r+',encoding='utf-8') as f: # Will open the douban File is written to douban.txt,encoding='utf-8' Specify the encoding method for
f.write(string) # write file
with open('douban.txt', mode='r',encoding='utf-8') as a:
print(a.read()) # Read the file The operation results are as follows :
open douban file # This content is written douban.txt The content in .2. Directory operation
stay python in , When specifying the file path , You need to match the path separator \ Transference , About... In the path \ Replace with \\. for example : For relative paths "demo\m.txt" Need to use "demo\\m.txt" Instead of . in addition , You can also use the path separator \ use / Instead of .
for example :
string = ' open douban file '
path = 'E:/PYTHON/CSDN/csdn/' # Specify the path
with open(path + 'douban.txt', mode='r+',encoding='utf-8') as f: # Will open the douban File is written to douban.txt,encoding='utf-8' Specify the encoding method for
f.write(string) # write file
with open(path + 'douban.txt', mode='r',encoding='utf-8') as a:
print(a.read()) # Read the file The operation results are as follows :
open douban file # This content is written douban.txt The content in .边栏推荐
- Table lock query and unlocking in SQL development part 1
- GRC concept GRC architecture RPC lifecycle
- Leetcode - number of operations, non repeating numbers, diagonal traversal, Joseph Ring
- 2022-07-28日报:Science:AI设计蛋白质再获突破,可设计特定功能性蛋白质
- Configure CX Oracle solution (cx_oracle.databaseerror) dpi-1047: cannot locate a 64 bit Oracle client library: "th
- MATLAB不覆盖导入EXCEL
- Opencv - cut out mask foreground area from grayscale image
- I heard that many merchants of crmeb have added the function of planting grass?
- What functions will be added to crmeb Standard Version 4.4
- Set structure byte alignment
猜你喜欢

Crmeb Standard Version window+phpstudy8 installation tutorial (I)

Endnote 与word关联

爆肝整理 JVM 十大模块知识点总结,不信你还不懂

Introduction to grpc

最小堆提升每次排序的效率

Baidu proposes a dynamic self distillation method to realize dense paragraph retrieval by combining interactive model and double tower model

【通道注意力机制】SENet

Crmeb Standard Edition window+phpstudy8 installation tutorial (III)

Daily question (retrospective)

What functions will be added to crmeb Standard Version 4.4
随机推荐
Table lock query and unlocking in SQL development part 1
Heuristic merging simple problem on tree
DataTables warning: table id=campaigntable - cannot reinitialize datatable. solve
Summary of common redis commands (self provided)
Rongyun real-time community solution
游戏测试的概念是什么?测试方法和流程有哪些?
800V高压系统
.net core 3.1版本配置跨域问题
How many of the top ten test tools in 2022 do you master
Differences between two ways of QT creating folders
全国211院校考研信息汇总整理
Explain the difference set, intersection set and union set of complex type set in detail.Net
Some operations of bit operation
封装统一返回对象MessageResult
Sharing of award-winning activities: you can get up to iphone13 after using WordPress to build your own blog
使用Mock技术帮助提升测试效率的小tips,你知道几个?
3、基本常数和宏定义
[delete specified number leetcode]
Pytorch - sequential and modulelist
Cross domain problems in the configuration of.Net core version 3.1