这篇博客主要说一下Python中文件的相关操作。
对于文件的操作可以说是通过几个比较重要的函数来完成的。
(1)open(参数1,参数2)
其中参数1是指要打开的文件名,参数2是指文件的打开方式
其中文件的打开方式有以下几种:
r 以只读的方式打开(默认)
w 以写入的方式打开文件,会覆盖已存在的文件
x 如果文件已经存在,使用此模式打开将引发异常
a 以写入模式打开,如果文件存在,则在末尾追加
b 以二进制的模式打开文件
t 以文本模式打开文件(默认)
+ 可读写模式(可添加到其他模式使用)
U 通用换行符支持
使用open函数会返回一个文件的对象,之后对于文件的操作都是在这个对象的基础上来实现的。
需要注意的是,使用默认的方式(即打开模式为只读)打开一个不存在的文件,会出现异常。而以写入的模式打开一个不存在的文件,系统会自动生成一个这样的文件。
例如一开始我的E盘下并没有这个文件,执行下面这条语句之后,就会产生文件没有找到的异常。
f=open("E:\\test.txt",'r')
而当使用下面的语句时,则会在E盘生成一个文件
f=open("E:\\test.txt",'w')
2.write()
作用:向文本中写入数据
例如向刚刚创建的文本中写入一行数据:
写完数据之后要及时将文件关闭,否则数据还是存储在缓冲区中,而不是写入文件中
f.write("这是我要向文件中写入的数据!!!") f.close()
3.read(参数1)
作用:从指定的文件中读取参数1指定个字符数,默认读取一个字符
例如从刚刚写入数据的文件中读取一个字符
f=open("E:\\test.txt",'w') f.write("这是我要向文件中写入的数据!!!") f.close() f=open("E:\\test.txt",'r')print(f.read(1))
4.官方推荐的读取文件内容的方法
f=open("E:\\text1.txt",'r')for eachline in f: print(eachline)
这就是python对于文件的简单操作,相比于Java中眼光缭乱的各种流简单多了,博客中的函数列举有限,只是列举了几个最常用的,如果有其他需求可以查Python官方文档或者是问度娘。再有新的知识我也会及时更新。
以上就是Python零基础入门之十一文件的内容,更多相关内容请关注PHP中文网(www.php.cn)!

The basic syntax for Python list slicing is list[start:stop:step]. 1.start is the first element index included, 2.stop is the first element index excluded, and 3.step determines the step size between elements. Slices are not only used to extract data, but also to modify and invert lists.

Listsoutperformarraysin:1)dynamicsizingandfrequentinsertions/deletions,2)storingheterogeneousdata,and3)memoryefficiencyforsparsedata,butmayhaveslightperformancecostsincertainoperations.

ToconvertaPythonarraytoalist,usethelist()constructororageneratorexpression.1)Importthearraymoduleandcreateanarray.2)Uselist(arr)or[xforxinarr]toconvertittoalist,consideringperformanceandmemoryefficiencyforlargedatasets.

ChoosearraysoverlistsinPythonforbetterperformanceandmemoryefficiencyinspecificscenarios.1)Largenumericaldatasets:Arraysreducememoryusage.2)Performance-criticaloperations:Arraysofferspeedboostsfortaskslikeappendingorsearching.3)Typesafety:Arraysenforc

In Python, you can use for loops, enumerate and list comprehensions to traverse lists; in Java, you can use traditional for loops and enhanced for loops to traverse arrays. 1. Python list traversal methods include: for loop, enumerate and list comprehension. 2. Java array traversal methods include: traditional for loop and enhanced for loop.

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
