关于python:文件处理如何使用Python实现写入文件代码示例

3次阅读

共计 3204 个字符,预计需要花费 9 分钟才能阅读完成。

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 file
file1 = open ('myfile.txt' , 'w')
L = ["This is Delhi n" , "This is Paris n" , "This is London n"]
s = "Hellon"
  
# Writing a string to file
file1.write(s)
  
# Writing multiple strings
# at a time
file1.writelines(L)
  
# Closing file
file1.close()
  
# Checking if the data is
# written to file or not
file1 = open ('myfile.txt' , 'r')
print (file1.read())
file1.close()

输入如下:

Hello
This is Delhi
This is Paris
This is London

追加到文件

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

# Python program to illustrate
# Append vs write mode
file1 = 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 last
file1 = open ("myfile.txt" , "a")  # append mode
file1.write("Today n")
file1.close()
  
file1 = open ("myfile.txt" , "r")
print ("Output of Readlines after appending")
print (file1.read())
print ()
file1.close()
  
# Write-Overwrites
file1 = open ("myfile.txt" , "w")  # write mode
file1.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 appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow

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 file
with open ("myfile.txt" , "w") as file1:
     # Writing data to a file
     file1.write("Hello n")
     file1.writelines(L)
  
# Reading from file
with open ("myfile.txt" , "r+") as file1:
     # Reading form a file
     print (file1.read())

输入如下:

Hello
This is Delhi
This is Paris
This 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
正文完
 0