Python 提供了用于创建、写入和读取文件的内置函数。python可以处理两种类型的文件,普通文本文件和二进制文件(用二进制语言编写,0和1)。
在本文中,我们将重点关注文本文件中的打开、关闭、读取和写入数据。
文件访问模式
访问模式控制打开的文件中可能进行的操作类型。它指的是文件打开后将如何使用。这些模式还定义文件句柄在文件中的位置。文件句柄就像游标,它定义必须从文件中的何处读取或写入数据。python中有6种访问模式。
文件如何加载到主内存中计算机中有两种内存,即主内存和辅助内存,您保存的每个文件或任何人保存的文件都位于辅助内存上,因为当计算机关闭电源时,主内存中的任何数据都会被删除。因此,当您需要更改任何文本文件或只是在 python 中使用它们时,您需要将该文件加载到主内存中。Python 通过“文件处理程序”**与主内存或主内存中加载的文件进行交互(这就是操作系统如何访问 python 来与您打开的文件进行交互,方法是在内存中搜索文件,如果找到它返回一个文件处理程序,然后您可以使用该文件)。
打开文件
这是使用 open() 函数完成的。该功能不需要导入任何模块。
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 append mode and file1 = open("MyFile1.txt","a") # store its reference in the variable file1 # and "MyFile2.txt" in D:\Text in file2 file2 = open(r"D:\Text\MyFile2.txt","w+")
此处,file1 创建为 MyFile1 的对象,file2 创建为 MyFile2 的对象
关闭文件
close() 函数关闭文件并释放该文件占用的内存空间。当不再需要该文件或要以不同的文件模式打开该文件时使用它。文件对象.close()
# Opening and Closing a file "MyFile.txt" # for object name file1. file1 = open("MyFile.txt","a") file1.close()
写入文件
写入文件有两种方法。
File_object.write(str1)
File_object.writelines(L) for L = [str1, str2, str3]
从文件中读取
可以通过三种方式从文本文件中读取数据。
File_object.read([n])
File_object.readline([n])
File_object.readlines()
注意: '\n'被视为两个字节的特殊字符
# Program to show various ways to read and # write data in a file. file1 = open("myfile.txt","w") L = ["This is Delhi \n","This is Paris \n","This is London \n"] # \n is placed to indicate EOL (End of Line) file1.write("Hello \n") file1.writelines(L) file1.close() #to change file access modes file1 = open("myfile.txt","r+") print("Output of Read function is ") print(file1.read()) print() # seek(n) takes the file handle to the nth # byte from the beginning. file1.seek(0) print( "Output of Readline function is ") print(file1.readline()) print() file1.seek(0) # To show difference between read and readline print("Output of Read(9) function is ") print(file1.read(9)) print() file1.seek(0) print("Output of Readline(9) function is ") print(file1.readline(9)) file1.seek(0) # readlines function print("Output of Readlines function is ") print(file1.readlines()) print() file1.close()
输出:
Output of Read function is Hello This is Delhi This is Paris This is London Output of Readline function is Hello Output of Read(9) function is Hello Th Output of Readline(9) function is Hello Output of Readlines function is ['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
附加到文件
# 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.readlines()) 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.readlines()) print() file1.close()
Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']
原文链接:codingdict.net