当前位置:网站首页>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 .边栏推荐
- Vs usage skills
- 解决pycharm使用powershell出错问题
- Multithreading
- 7、实时数据备份和实时时钟相关定义
- Nftscan and nftplay have reached strategic cooperation in the field of NFT data
- [leetcode] binary search given an N-element ordered (ascending) integer array num and a target value target, write a function to search the target in num. if the target value exists, return the subscr
- Baidu proposes a dynamic self distillation method to realize dense paragraph retrieval by combining interactive model and double tower model
- 堆操作
- What are the functions to be added in crmeb pro2.2?
- Pytorch - autograd automatic differentiation
猜你喜欢

爬虫入门(1)——requests(1)

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

ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching

Crmeb Standard Version window+phpstudy8 installation tutorial (I)

Grpc protocol buffer

简单入手Swagger

Tencent interview -- please design a thread pool to implement sequential execution

flowable工作流所有业务概念

机器学习的3大“疑难杂症”,因果学习是突破口 | 重庆大学刘礼

Nftscan and nftplay have reached strategic cooperation in the field of NFT data
随机推荐
22. Realization of message processing task
GRC concept GRC architecture RPC lifecycle
800V高压系统
Here comes the full open source free customer service system
Celery related
What functions will be added to crmeb Standard Version 4.4
爆肝整理 JVM 十大模块知识点总结,不信你还不懂
堆操作
Endnote 与word关联
QCustomPlot绘图工具常用方法
I heard that many merchants of crmeb have added the function of planting grass?
Close independent windows and close other windows at the same time
ECCV 2022 | SSP: 自支持匹配的小样本任务新思想
Deepfacelab model parameters collection
Common methods of qcustomplot drawing tools
Leetcode - number of operations, non repeating numbers, diagonal traversal, Joseph Ring
19. Channel assignment task definition
手把手带你编写一个规范的字符设备驱动
10、相关数据累积任务实现
软件测试的流程规范有哪些?具体要怎么做?