当前位置: 首页 > news >正文

做兼职什么网站网站设计公司模板

做兼职什么网站,网站设计公司模板,用自家宽带做网站服务器,wordpress二维码管理插件下载目录 基本步骤 常用函数 open()函数 close()函数 read()函数 readlines()函数 readline()函数 write()函数 writelines()函数 with语句 读写操作的应用: 拷贝文件 with 语句的嵌套 逐行拷贝 基本步骤 1. 打开文件:open(filepath, mode, en…

目录

基本步骤

常用函数

open()函数

close()函数

read()函数

readlines()函数

readline()函数

write()函数

writelines()函数

with语句

读写操作的应用:

拷贝文件

 with 语句的嵌套

逐行拷贝


基本步骤

1. 打开文件:open(filepath, mode, encoding)

2. 读写文件:read() / write()

3. 关闭文件:close()

python读取文件操作实例
f = open('filename.txt', 'r', encoding='utf-8')
f.read()
f.close()

常用函数

open()函数

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

file是要打开的文件,mode='r'是打开文件的模式,encoding是编码格式

文件的打开模式有以下字符表示:

    'r'       open for reading (default)
    'w'       open for writing, truncating the file first
    'x'       create a new file and open it for writing
    'a'       open for writing, appending to the end of the file if it exists
    'b'       binary mode
    't'       text mode (default)
    '+'       open a disk file for updating (reading and writing)

打开模式还能连用:

The default mode is 'rt' (open for reading text). For binary random  access, the mode 'w+b' opens and truncates the file to 0 bytes, while 'r+b' opens the file without truncation. The 'x' mode implies 'w' and raises an `FileExistsError` if the file already exists.

close()函数

close() method of _io.TextIOWrapper instance
    Flush and close the IO object.

    This method has no effect if the file is already closed

打开文件,必须有对应的关闭,否则该未关闭的文件不能被其它的应用操作。

read()函数

read(size=-1, /) method of _io.TextIOWrapper instance
    Read at most size characters from stream.

    Read from underlying buffer until we have size characters or we hit EOF.
    If size is negative or omitted, read until EOF.

read()可选参数size可以用于读取“size”大小的数据,返回的是字符串或字节对象,若是size的值没有填写,或者是个负值,那么read()函数将读取文件的所有内容,这也是用python开发pc软件中“复制黏贴”的功能比较常用的函数。

readlines()函数

Help on built-in function readlines:

readlines(hint=-1, /) method of _io.TextIOWrapper instance
    Return a list of lines from the stream.

    hint can be specified to control the number of lines read: no more
    lines will be read if the total size (in bytes/characters) of all
    lines so far exceeds hint.

readlines()函数是将文件当中的所有行,一行一行地读取,并逐一写入一个列表list内,最终返回这个列表。

readline()函数

Help on built-in function readline:

readline(size=-1, /) method of _io.TextIOWrapper instance
    Read until newline or EOF.

    Return an empty string if EOF is hit immediately.
    If size is specified, at most size characters will be read.

readline()函数就是读取一行数据,用法除了size参数之外,就跟read()差不多,也是open()打开文件,readline()读取数据,close()关闭文件

write()函数

write(text, /) method of _io.TextIOWrapper instance
    Write string s to stream.

    Return the number of characters written
    (which is always equal to the length of the string).

writelines()函数

writelines(lines, /) method of _io.TextIOWrapper instance
    Write a list of lines to stream.

    Line separators are not added, so it is usual for each of the
    lines provided to have a line separator at the end.

with语句

通过with语句,不管读取还是写入文件操作都不用写对应的close()函数,语句块结束系统会自动关闭文件。

with open('filename.txt', 'r', encoding='urf-8') as f:f.read()

读写操作的应用:

拷贝文件

# 打开源文件以读取内容  
with open('source.txt', 'r') as source_file:  source_content = source_file.read()  # 打开目标文件以写入内容  
with open('destination.txt', 'w') as destination_file:  destination_file.write(source_content)

 with 语句的嵌套

以上两个with语句块还能嵌套写在一起:

# 打开源文件以读取内容,并同时打开目标文件以写入内容  
with open('source.txt', 'r') as source_file:  with open('destination.txt', 'w') as destination_file:  # 读取源文件的内容  source_content = source_file.read()  # 将读取的内容写入到目标文件中  destination_file.write(source_content)

可以写在同一行:

# 打开源文件以读取内容,并同时打开目标文件以写入内容  
with open('source.txt', 'r') as source_file, open('destination.txt', 'w') as destination_file:  # 读取源文件的内容  source_content = source_file.read()  # 将读取的内容写入到目标文件中  destination_file.write(source_content)

逐行拷贝

如果源文件很大,使用read()方法一次性读取所有内容可能会消耗较多的内存。对于大文件,更推荐的做法是使用文件对象的迭代器逐行读取和写入,这样可以减少内存的使用。

with open('source.txt', 'r') as source_file, open('destination.txt', 'w') as destination_file:  # 逐行读取源文件并写入目标文件  for line in source_file:  destination_file.write(line)

http://www.dt0577.cn/news/10096.html

相关文章:

  • 马尼拉做网站谷歌搜索引擎免费入口 香港
  • 青岛商媒做网站怎么样电商平台怎么做
  • 哪里可以学做网站产品市场营销策划书
  • 做的网站怎样评估价值seo渠道
  • 上海网站建设hxwlkj长沙做引流推广的公司
  • wordpress做电商网站免费下载百度app最新版本
  • 广州网站建设360元国产最好的a级suv88814
  • 做微信的网站叫什么软件千锋教育官方网
  • wordpress 批量建站全球外贸b2b网站
  • 深圳专业做网站排名公司学校招生网络营销方案
  • 房地产楼盘微信网站建设营销方案漂亮的网页设计
  • 卖产品的网站怎么做响应式网站模板的优势
  • 专业做外贸网站建设关键词seo优化公司
  • 颍泉网站建设百度帐号
  • 石家庄网站建设推广服务免费外网加速器
  • 建网站教程如何快速推广自己的品牌
  • 孝感企业做网站网络推广外包代理
  • 天河做网站服务seo基础培训机构
  • 南昌加盟网站制作成人英语培训班哪个机构好
  • 以下什么是常用的可视化网页制作工具新乡网站seo
  • wordpress 分页 美化短视频seo推广隐迅推专业
  • 视频网站 怎么做北京网站建设公司案例
  • 建网站注意什么aso优化服务平台
  • 网站建设学习什么怎么做好网络营销推广
  • 做彩票网站是违法吗搜索引擎实训心得体会
  • 如何做网站规范影响关键词优化的因素
  • 网站源码程序下载谷歌seo搜索优化
  • 哪个网站有利于做课件长沙靠谱seo优化价格
  • 手机网站设计尺寸如何推广自己产品
  • 阿里巴巴吧做网站广州seo服务外包