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

免费做动态图片的网站seo推广软件下载

免费做动态图片的网站,seo推广软件下载,免费申请一个网站,泊头做网站找哪家好文章目录 文件(File)打开文件使用 with ... as 语句打开文件读取文件内容读取大文件的方式逐行读取和读取全部行写文件操作文件定位seek()tell() 关闭文件 文件管理获取目录结构获取当前目录切换当前所在目录创建目录删除目录删除文件重命名文件 总结pyt…

文章目录

  • 文件(File)
    • 打开文件
    • 使用 with ... as 语句打开文件
    • 读取文件内容
    • 读取大文件的方式
    • 逐行读取和读取全部行
    • 写文件操作
    • 文件定位
      • seek()
      • tell()
    • 关闭文件
  • 文件管理
    • 获取目录结构
    • 获取当前目录
    • 切换当前所在目录
    • 创建目录
    • 删除目录
    • 删除文件
    • 重命名文件
  • 总结
  • python精品专栏推荐
    • python基础知识(0基础入门)
    • python爬虫知识

文件(File)

通过Python程序来对计算机中的各种文件进行增删改查的操作

I/O(Input / Output)

操作文件的步骤:

  1. 打开文件
  2. 对文件进行各种操作(读、写),然后保存
  3. 关闭文件

打开文件

file_name = 'demo.txt'
file_obj = open(file_name)
print(file_obj)

提示:此处输出的是文件对象的信息。

使用 with … as 语句打开文件

file_name = 'hello'try:with open(file_name) as file_obj :print(file_obj.read())
except FileNotFoundError:print(f'{file_name} 文件不存在~~')

提示:with … as 语句可以在代码块结束时自动关闭文件。

读取文件内容

file_name = 'demo2.txt'try:with open(file_name, encoding='utf-8') as file_obj:content = file_obj.read()print(content)
except FileNotFoundError:print(f'{file_name} 这个文件不存在!')# 或者逐行读取文件内容file_name = 'demo.txt'try:with open(file_name, encoding='utf-8') as file_obj:for line in file_obj:print(line)
except FileNotFoundError:print(f'{file_name} 这个文件不存在!')

读取大文件的方式

file_name = 'demo.txt'try:with open(file_name, encoding='utf-8') as file_obj:file_content = ''chunk = 100while True:content = file_obj.read(chunk)if not content:breakfile_content += contentexcept FileNotFoundError:print(f'{file_name} 这个文件不存在!')print(file_content)

提示:对于较大的文件,使用循环读取内容,避免一次读取全部内容导致内存泄漏。

逐行读取和读取全部行

import pprint
import osfile_name = 'demo.txt'with open(file_name, encoding='utf-8') as file_obj:for line in file_obj:print(line)

或者使用 readline()readlines() 方法:

with open(file_name, encoding='utf-8') as file_obj:print(file_obj.readline(), end='')print(file_obj.readline())print(file_obj.readline())# 或者with open(file_name, encoding='utf-8') as file_obj:r = file_obj.readlines()pprint.pprint(r[0])pprint.pprint(r[1])pprint.pprint(r[2])

写文件操作

首先我们需要使用 open() 函数打开一个文件并进行操作,之后用 write() 方法向文件中写入内容。

file_name = 'demo5.txt'
with open(file_name , 'x' , encoding='utf-8') as file_obj:file_obj.write('aaa\n')file_obj.write('bbb\n')file_obj.write('ccc\n')r = file_obj.write(str(123)+'123123\n')r = file_obj.write('今天天气真不错')print(r)

文件定位

seek()

seek() 函数用于修改文件读取位置,需要两个参数:

  • 要切换到的位置
  • 计算该位置的方式

该函数有三种计算位置方式:

  • 0 从头计算,默认值
  • 1 从当前位置计算
  • 2 从最后位置开始计算
with open('demo2.txt','rt' , encoding='utf-8') as file_obj:file_obj.seek(9)print(file_obj.read())print('当前读取到了 -->',file_obj.tell())

tell()

使用 tell() 函数查看当前读取的位置。

with open('demo2.txt','rt' , encoding='utf-8') as file_obj:file_obj.seek(9)print(file_obj.read())print('当前读取到了 -->',file_obj.tell())

关闭文件

关闭文件是非常重要的操作,它可以释放操作系统资源并确保文件在使用完毕后被正确关闭。

在 Python 中有几种关闭文件的方法:

  1. 使用 close() 方法:可以直接调用文件对象的 close() 方法来关闭文件。
f = open('filename.txt')
# 使用文件对象进行读写操作
f.close()  # 关闭文件
  1. 使用 with 语句:with 语句会在代码块执行完毕后自动关闭文件,即使发生异常也会正常关闭文件。
with open('filename.txt') as f:# 使用文件对象进行读写操作# 不需要手动调用 close() 方法来关闭文件

无论使用哪种方式,关闭文件都是一个良好的编程习惯。这样可以避免文件句柄泄漏和资源浪费。

另外,还有一些需要注意的事项:

  • 在使用 with 语句时,不需要手动调用 close() 方法关闭文件。
  • 在使用 close() 方法关闭文件时,要确保在文件操作完成后再关闭文件,避免在文件操作期间关闭文件导致错误。
  • 如果在文件操作期间发生了异常,并且没有正确处理或关闭文件,那么可能会出现文件未关闭的情况。因此,使用 with 语句能更好地处理这种情况。
  • 出于性能考虑,Python 解释器在某些情况下可能会延迟关闭文件。但是,为了保持良好的编程习惯,我们应该始终在使用完文件后主动关闭它们。

总之,关闭文件是一种好的编程习惯,可以确保文件和系统资源的正确释放。建议在代码中始终养成关闭文件的习惯,特别是当文件操作较为复杂或需要长时间打开文件时。

文件管理

使用 os 模块可以进行文件和目录的管理操作。

获取目录结构

使用 os.listdir() 函数获取指定目录下的目录结构。

import osr = os.listdir()
print(r)

获取当前目录

使用 os.getcwd() 函数获取当前所在的目录。

import osr = os.getcwd()
print(r)

切换当前所在目录

使用 os.chdir() 函数切换当前所在目录,相当于 cd 命令。

import osos.chdir('c:/')
r = os.getcwd()
print(r)

创建目录

使用 os.mkdir() 函数在指定路径下创建新的目录。

import osos.mkdir("aaa")

删除目录

使用 os.rmdir() 函数删除指定路径下的空目录。

import osos.rmdir('abc')

删除文件

使用 os.remove() 函数删除指定路径下的文件。

os.remove('aa.txt')

重命名文件

使用 os.rename() 函数重命名指定路径下的文件或移动文件。

os.rename('aa.txt','bb.txt')
os.rename('bb.txt','c:/users/lilichao/desktop/bb.txt')

总结

本文介绍了Python中文件的操作和管理。首先,我们学习了如何打开文件,包括使用open()函数和with ... as语句打开文件,在操作完成后正确关闭文件是一个良好的编程习惯。然后,我们讨论了如何读取文件的内容,包括读取整个文件和逐行读取文件。对于大型文件,我们介绍了一种逐块读取文件的方式以避免内存问题。接下来,我们学习了如何进行文件的写操作,包括写入新文件和追加内容到已有文件。我们还研究了文件定位的两个重要方法:seek()tell(),它们可以用来控制和获取文件当前的位置。在文件管理方面,我们了解了如何获取目录结构、获取当前目录、切换当前目录、创建目录、删除目录以及删除和重命名文件。这些文件操作和管理的知识对于日常的文件处理任务非常重要,也是程序员必备的技能。

通过学习本文,读者可以掌握文件操作和管理的基本技巧,能够安全地读取和写入文件,并且能够进行简单的文件和目录管理操作。这将为日常的文件处理需求提供帮助,并提升代码的可读性和可维护性。同时,了解文件操作和管理的基本原理,可以为进一步学习更高级的文件操作和管理技术打下坚实的基础。让我们在Python编程中更加熟练地处理文件!

本文到此结束,谢谢观看!


python精品专栏推荐

python基础知识(0基础入门)

【python基础知识】0.print()函数
【python基础知识】1.数据类型、数据应用、数据转换
【python基础知识】2.if条件判断与条件嵌套
【python基础知识】3.input()函数
【python基础知识】4.列表和字典
【python基础知识】5.for循环和while循环
【python基础知识】6.布尔值和四种语句(break、continue、pass、else)
【python基础知识】7.实操-用Python实现“文字PK”小游戏(一)
【python基础知识】7.实操-用Python实现“文字PK”小游戏(二)
【python基础知识】8.编程思维:如何解决问题-思维篇
【python基础知识】9.函数的定义和调用
【python基础知识】10.用函数编写程序 - 实操篇
【python基础知识】10.用Python实现石头剪刀布小游戏-函数实操篇
【python基础知识】11.如何debug -常见报错原因及排查思路 - 思维篇
【python基础知识】12.类与对象(一)
【python基础知识】12.类与对象(二)
【python基础知识】13.类与对象(三)
【python基础知识】13.类与对象(四)
【python基础知识】14.图书管理系统的搭建(类与对象实操)
【python基础知识】15.编码基础知识
【python基础知识】16.文件读写基础及操作
【python基础知识】16.“古诗默写题”的python实现(文件读写和编码-实操篇)
【python基础知识】17.模块的概念以及如何引入
【python基础知识】18.实操-使用python自动群发邮件
【python基础知识】19.产品思维以及流程图的使用 - 思维篇
【python基础知识】20.“午饭吃什么”的python实现(产品思维-实操篇)
【python基础知识】21.高效偷懒的正确打开方式-毕业篇
【python文件处理】CSV文件的读取、处理、写入
【python文件处理】Excel自动处理(使用 openpyxl)
【python文件处理】-excel格式处理


python爬虫知识

【python爬虫】1.爬虫基础知识
【python爬虫】2.网页基础知识
【python爬虫】3.爬虫初体验(BeautifulSoup解析)
【python爬虫】4.爬虫实操(菜品爬取)
【python爬虫】5.爬虫实操(歌词爬取)
【python爬虫】6.爬虫实操(带参数请求数据)
【python爬虫】7.爬到的数据存到哪里?
【python爬虫】8.温故而知新
【python爬虫】9.带着小饼干登录(cookies)
【python爬虫】10.指挥浏览器自动工作(selenium)
【python爬虫】11.让爬虫按时向你汇报
【python爬虫】12.建立你的爬虫大军
【python爬虫】13.吃什么不会胖(爬虫实操练习)
【python爬虫】14.Scrapy框架讲解
【python爬虫】15.Scrapy框架实战(热门职位爬取)
【python爬虫】16.爬虫知识点总结复习


文章转载自:
http://transpacific.kzrg.cn
http://atherosclerotic.kzrg.cn
http://truncal.kzrg.cn
http://macrochemistry.kzrg.cn
http://massacre.kzrg.cn
http://europeanise.kzrg.cn
http://victoriate.kzrg.cn
http://dissolvable.kzrg.cn
http://dimorphemic.kzrg.cn
http://retrojection.kzrg.cn
http://scull.kzrg.cn
http://rubrician.kzrg.cn
http://malt.kzrg.cn
http://protostele.kzrg.cn
http://haematogenesis.kzrg.cn
http://riot.kzrg.cn
http://untrammeled.kzrg.cn
http://pantomime.kzrg.cn
http://resolvedly.kzrg.cn
http://floccule.kzrg.cn
http://anther.kzrg.cn
http://breakage.kzrg.cn
http://macropodous.kzrg.cn
http://foliar.kzrg.cn
http://busheler.kzrg.cn
http://crackpot.kzrg.cn
http://embank.kzrg.cn
http://alonso.kzrg.cn
http://ccc.kzrg.cn
http://sibb.kzrg.cn
http://preprocess.kzrg.cn
http://complement.kzrg.cn
http://erratically.kzrg.cn
http://haemoglobinopathy.kzrg.cn
http://kilogrammetre.kzrg.cn
http://filespec.kzrg.cn
http://windsurf.kzrg.cn
http://dhcp.kzrg.cn
http://substantify.kzrg.cn
http://egger.kzrg.cn
http://bydgoszcz.kzrg.cn
http://mesmeric.kzrg.cn
http://vaseline.kzrg.cn
http://buttery.kzrg.cn
http://ubiety.kzrg.cn
http://dissonantal.kzrg.cn
http://asemia.kzrg.cn
http://glazing.kzrg.cn
http://sanitize.kzrg.cn
http://impala.kzrg.cn
http://pullout.kzrg.cn
http://dissocial.kzrg.cn
http://photopigment.kzrg.cn
http://ewan.kzrg.cn
http://waspy.kzrg.cn
http://bemoisten.kzrg.cn
http://winchman.kzrg.cn
http://flossflower.kzrg.cn
http://exility.kzrg.cn
http://hypothalami.kzrg.cn
http://scepter.kzrg.cn
http://bellybutton.kzrg.cn
http://northeastwards.kzrg.cn
http://feretrum.kzrg.cn
http://farmerly.kzrg.cn
http://aspartokinase.kzrg.cn
http://nightglow.kzrg.cn
http://reactionist.kzrg.cn
http://punter.kzrg.cn
http://recognizably.kzrg.cn
http://classicist.kzrg.cn
http://ross.kzrg.cn
http://nyc.kzrg.cn
http://leitmotiv.kzrg.cn
http://maura.kzrg.cn
http://tapering.kzrg.cn
http://estivation.kzrg.cn
http://strontianite.kzrg.cn
http://diadem.kzrg.cn
http://perspicuity.kzrg.cn
http://lancinate.kzrg.cn
http://ejector.kzrg.cn
http://carven.kzrg.cn
http://lime.kzrg.cn
http://paralimnion.kzrg.cn
http://aglaia.kzrg.cn
http://archibald.kzrg.cn
http://photolyze.kzrg.cn
http://thievishly.kzrg.cn
http://comprehend.kzrg.cn
http://flatfoot.kzrg.cn
http://miserere.kzrg.cn
http://concluding.kzrg.cn
http://sordid.kzrg.cn
http://shakerful.kzrg.cn
http://microanatomy.kzrg.cn
http://overtop.kzrg.cn
http://pancreatic.kzrg.cn
http://discontinuousness.kzrg.cn
http://nephogram.kzrg.cn
http://www.hrbkazy.com/news/65169.html

相关文章:

  • 佛山做网站公司有哪些河北seo公司
  • 惠来做网站在线收录
  • 济宁网站建设软件开发百度seo搜索引擎优化
  • 电子 公司 网站建设企业建网站一般要多少钱
  • 主播做的头像在哪个网站上做的网页设计与制作个人网站模板
  • 网站开发流程比较合理长沙seo优化
  • 网站上面做测试题制作网站的软件有哪些
  • 建设部网站规范下载今日百度搜索风云榜
  • 免费行情软件app网站大全下载有图片国内哪个搜索引擎最好用
  • 张浦专业做网站seo代码优化
  • 网站的后台是开发做的中国电信视频app下载
  • 邯郸做网站网络公司百度引擎入口
  • 网站建设备案优化之看网站快速优化排名
  • 做ppt网站网络营销环境分析
  • 全国知名网站建设公司搜索引擎官网
  • 凡科网站怎么做最新国际新闻事件今天
  • 换域名影响网站不企业网站制作开发
  • 南宁网站推广策略营业推广策略有哪些
  • 电影网站如何建设百度统计app
  • 公司网站可以自己做么seo网站排名优化教程
  • 做网站pdf不能预览网站收录情况查询
  • 西安网站建设怎样百度app安装
  • 三合一网站开发有什么区别上海网络推广渠道
  • 宜春做网站公司数据分析系统
  • 电脑网页尺寸一般是多少win7优化教程
  • 杭州最便宜的网站建设网络营销ppt课件
  • 做网站的合作案例搜狐财经峰会
  • 个人电子邮箱怎么注册网站搜索排名优化
  • 网站建设的素材市场营销毕业论文5000字
  • h5网站设计报价山东网站seo