Python提供了用于创立, 写入和读取文件的内置函数。能够在python中解决的文件有两种类型, 一般文本文件和二进制文件(以二进制语言0s和1s编写)。

  • 文本文件:在这种类型的文件中, 每行文本都以称为EOL(行尾)的特殊字符终止, 默认状况下, 该字符是python中的换行符( n)。
  • 二进制文件:在这种类型的文件中, 一行没有终结符, 并且在将数据转换为机器可了解的二进制语言后将其存储。

留神:进一步理解文件解决点击这里.

目录拜访形式关上文件敞开文件写入文件追加文件With语句

文件存取形式

拜访模式管制着关上的文件中可能的操作类型。指的是关上文件后的应用形式。这些模式还定义了文件句柄在文件中的地位。文件句柄就像一个游标, 它定义了必须从何处读取或写入文件中的数据。读取文件的不同拜访形式为–

  1. 只写(w):关上文件进行写入。对于现有文件, 数据将被截断并被笼罩。句柄位于文件的结尾。如果文件不存在, 则创立文件。
  2. 读写(" w +"):关上文件进行读写。对于现有文件, 数据将被截断并被笼罩。句柄位于文件的结尾。
  3. 仅附加('a'):关上文件进行写入。如果文件不存在, 则创立该文件。句柄位于文件的开端。正在写入的数据将插入到现有数据的开端。

留神:进一步理解拜访模式点击这里.

关上文件

它是应用关上()性能。此性能不须要导入任何模块。

语法如下:

File_object = open(r"File_Name", "Access_Mode")

该文件应与python程序文件位于同一目录中, 否则, 应将文件的残缺地址写在文件名的地位。

留神:的[R放在文件名之前, 以避免文件名字符串中的字符被视为特殊字符。例如, 如果文件地址中有 temp, 则 t被视为制表符, 并且会引发有效地址的谬误。 r使字符串原始, 即, 它表明该字符串没有任何特殊字符。如果文件位于同一目录中并且未搁置地址, 则能够疏忽r。

# Open function to open the file "MyFile1.txt"  # (same directory) in read mode and file1 = open ( "MyFile.txt" , "w" )     # store its reference in the variable file1  # and "MyFile2.txt" in D:Text in file2 file2 = open (r "D:TextMyFile2.txt" , "w+" )

在这里, 文件1创立为MyFile1的对象, 文件2创立为MyFile2的对象。

敞开文件

关()函数敞开文件并开释该文件获取的内存空间。在不再须要文件或要以其余文件模式关上文件时应用它。

语法如下:

File_object.close()
# Opening and Closing a file "MyFile.txt" # for object name file1. file1 = open ( "MyFile.txt" , "w" ) file1.close()

写入文件

有两种写入文件的办法。

  1. write():将字符串str1插入文本文件的一行中。

    File_object.write(str1)

  2. writelines():对于字符串元素列表, 每个字符串都插入到文本文件中。用于一次插入多个字符串。

    File_object.writelines(L) for L = [str1, str2, str3]

留神: ‘ n’被视为两个字节的特殊字符。

例子:

# Python program to demonstrate# writing to file  # Opening a filefile1 = open ( 'myfile.txt' , 'w' )L = [ "This is Delhi n" , "This is Paris n" , "This is London n" ]s = "Hellon"  # Writing a string to filefile1.write(s)  # Writing multiple strings# at a timefile1.writelines(L)  # Closing filefile1.close()  # Checking if the data is# written to file or notfile1 = open ( 'myfile.txt' , 'r' )print (file1.read())file1.close()

输入如下:

HelloThis is DelhiThis is ParisThis is London

追加到文件

在追加模式下关上文件时, 句柄位于文件的开端。正在写入的数据将插入到现有数据的开端。让咱们看上面的示例, 以说明写模式和追加模式之间的区别。

# Python program to illustrate# Append vs write modefile1 = open ( "myfile.txt" , "w" )L = [ "This is Delhi n" , "This is Paris n" , "This is London n" ]file1.writelines(L)file1.close()  # Append-adds at lastfile1 = open ( "myfile.txt" , "a" )  # append modefile1.write( "Today n" )file1.close()  file1 = open ( "myfile.txt" , "r" )print ( "Output of Readlines after appending" )print (file1.read())print ()file1.close()  # Write-Overwritesfile1 = open ( "myfile.txt" , "w" )  # write modefile1.write( "Tomorrow n" )file1.close()  file1 = open ( "myfile.txt" , "r" )print ( "Output of Readlines after writing" )print (file1.read())print ()file1.close()

输入如下:

Output of Readlines after appendingThis is DelhiThis is ParisThis is LondonTodayOutput of Readlines after writingTomorrow

with语句

与Python中的语句用于异样解决, 以使代码更整洁, 更易读。它简化了对诸如文件流之类的公共资源的治理。与上述实现不同, 无需调用file.close()与with语句一起应用时。的与语句自身可确保正确获取和开释资源。

语法如下:

with open filename as file:
# Program to show various ways to# write data to a file using with statement  L = [ "This is Delhi n" , "This is Paris n" , "This is London n" ]  # Writing to filewith open ( "myfile.txt" , "w" ) as file1:     # Writing data to a file     file1.write( "Hello n" )     file1.writelines(L)  # Reading from filewith open ( "myfile.txt" , "r+" ) as file1:     # Reading form a file     print (file1.read())

输入如下:

HelloThis is DelhiThis is ParisThis is London

更多Python开发相干内容请参考:lsbin - IT开发技术:https://www.lsbin.com/

查看以下更多Python相干的内容:

  • Python pytube实现下载视频:https://www.lsbin.com/3450.html
  • Python过程的同步和池化:https://www.lsbin.com/3326.html
  • Python VSCode Excel关上CSV乱码解决:https://www.lsbin.com/2958.html