Python's File handling
1. Common Operations for files
Files are commonly used in daily programming, and are typically useful for storing data or application-system parameters. Python provides the OS, Os.path, Shutil, and other modules to handle files, including the most commonly used open files, read and write files, assignment files and delete files and other functions.
1.1 Creation of files
The global file () function in the Python2 is removed from the python3.+, and the open () function is retained. Open or create a file can use the function open (). The function can specify the processing mode, set the open file as read-only, write-only, read-write status. The declaration of Open () is as follows:
open(file, mode=‘r‘, buffering=None, encoding=None, errors=None, newline=None, closefd=True)
Parameter description:
- The parameter file is the name of the file that is opened, and if the file does not exist, open () creates a file named name and then opens the file.
- The parameter mode refers to the open mode of the file. Please refer to the following for the opening mode of the file.
- The parameter buffering is the set cache mode. 0 means no cache, 1 is cache, or greater than 1 indicates the size of the buffer, in bytes.
- The parameter encoding is the character encoding format of the settings file.
The open () function returns a file object that can be used to manipulate file patterns for files:
' R ' Open for reading (default)
' W ' open for writing, truncating the file first
' x ' Create a new file and open it for writing
Create a new file, open and write
' A ' open for writing, appending to the end of the file if it exists
Mode opens the file to append
' B ' binary mode binary modes open, can be used in conjunction with other modes
' t ' text mode (default)
' + ' open a disk file for updating (reading and writing)
' U ' universal newline mode (deprecated) supports all line break symbols
Note: Images, videos, and other files must be read and written using the B mode.
message = ‘‘‘hello world,\nhello python,\ngood time.‘‘‘f = open(‘test.txt‘,‘w‘)f.write(message)f.close()
Code Description:
- Define a String variable
- Creates a file in write-only mode and writes
- Writing a string variable to a file
- Close File
1.2 Reading of the file
There are several ways to read a file, which can be read using ReadLine (), ReadLines (), or the read () function.
- Read by row ReadLine ()
ReadLine () reads a file one line at a time, and it needs to be looped. However, when the file pointer moves to the end of the file, the file will still be read with ReadLine () and an error occurs. Therefore, you need to add a judgment statement in the program to determine if the time pointer is at the end of the file and break the loop through the statement. Examples are as follows:
# 使用readline模式读取文件f = open(‘test.txt‘,‘r‘)while True: line = f.readline() if line: print(line) else: breakf.close()#如果line = f.readline(2)则表示每次循环只读取两字节的内容,直到行的末尾
2. Multi-line read mode ReadLines ()
# 多行读取文件f = open(‘test.txt‘)lines = f.readlines()for line in lines: print(line)f.close()
3. One-time reading mode read ()
The simplest way to read a file is to use Read (), read () reads all the contents of the file at once, and assigns a value to the string variable, but when the file is larger, it is not recommended to read the file in a way that reads a large amount of memory and affects the performance of the system. Examples are as follows:
# 一次读取文件f = open(‘test.txt‘,‘r‘)lines = f.read()print(lines)f.close()
File pointers:
with open(‘test.txt‘,‘rb‘) as src: rd = src.read(100) print(rd) print(src.seek(src.tell())) rd = src.read(100) print(rd) print(src.seek(src.tell()))#每次读取100字节,然后返回指针的位置
4.with function
Usually we use open () and assign values to a string variable to manipulate the file, and finally we need to manually close the file, so it is a bit cumbersome to write, we can use the WITH function to open and close the file on a line of functions.
with open(‘test.txt‘,‘r‘) as src: da = src.read() print(da)#只读模式打开文件并赋值给src,然后对文件进行操作即可,代码与使用open()来操作文件相同。
1.3 Writing of files
There are several ways to write a file, you can use write (), or you can write to a file using the Writelines () method. Write () writes a string to the file, and Writelines () writes the list to the file. Examples are as follows:
m1 = ‘hello world‘l1 = [‘good‘,‘time‘]f = open(‘test1.txt‘,‘w‘)f.write(m1)f.writelines(l1)f.close()
Append to File:
m1 = ‘hello python‘f = open(‘test1.txt‘,‘a+‘)f.write(m1)f.close()
1.4 Deletion of files
The deletion of files requires the use of OS modules and Os.path modules, and the OS module provides the system's environment, files, directories and other operating system functions. The functions of the OS module that are more commonly used for files are as follows:
- Os.access (Path,mode) #按照mode指定的权限进行访问
- Os.chmod (Path,mode) #改变文件的访问权限, mode is represented by the UNIX permission symbol
- Os.open (filename,flag[,mode=0777]) #按照mode指定的权限打开文件. By default, read, write, and execute permissions to all users
- Os.remove (PATH) #删除path指定的文件
- Os.rename (old,new) #重命名文件或目录, old indicates the original file or directory, new represents the file or directory
- Os.stat (PATH) #返回path指定文件的所有属性
- Os.fstat (PATH) #返回打开文件的所有属性
- Os.startfile (Filepath[,operation]) #启动关联程序打开文件. For example, open an HTML file that will start IE browser
- Os.tmpfile () #创建一个临时文件, files are created in the operating system's TEMP directory
Note: The open () function of the OS module differs from the use of the built-in open () function.
The functions commonly used by the Os.path module are as follows:
- Os.path.abspath (PATH) #返回path所在的绝对路径
- Os.path.dirpath (PATH) #返回目录的路径
- Os.path.exists (PATH) #判断文件是否存在
- Os.path.getatime (filename) #返回文件的最后访问时间
- Os.path.getctime (filename) #返回文件的创建时间
- Os.path.getmtime (filename) #返回文件最后的修改时间
- Os.path.getsize (filename) #返回文件的大小
Os.path judgment function
- Os.path.isabs (s) #测试路径是否是绝对路径
- Os.path.isdir (PATH) #判断path指定的是否是目录
- Os.path.isfile (PATH) #判断path指定的是否是文件
- Os.path.split (p) #对路径进行分割 and returns as a list
- Os.path.splitext (P) #从路径中分割文件的扩展名
- Os.path.splitdrive (P) #从路径中分割驱动器的名称
- Os.walk (Top,func,arg) #遍历目录树
Examples are as follows:
import osif os.path.exists(‘../test.txt‘): os.remove(‘test.txt‘) print(‘is del‘)else: print(‘no‘)
1.5 Copying of files
There are several ways to copy files, let's look at the first way to compare low, that is, read and write the way to copy files. Examples are as follows:
#使用read()、write()实现文件复制f1 = open(‘1.txt‘,‘r‘)f2 = open(‘2.txt‘,‘w‘)f2.write(f1.read())f2.close()f1.close()
The second method:
Shutil module, the Shutil module is another management interface for files and directories, and provides some functions for copying and directory. The CopyFile () function can implement a copy of the file, and the declaration of the CopyFile () function is as follows:
shuil.copyfile(src,dst)
- SRC represents the path of the source file, SRC is a string type
- DST represents the path to the destination file, and DST is a string type
- SRC points to files that are copied to the DST point
Examples are as follows:
import shutilshutil.move(‘1.txt‘,‘2.txt‘)
1.6 Renaming of files
The function rename () of the OS module can rename a file or directory.
import osos.rename(‘1.txt‘,‘11.txt‘)
You can also use the move () function in shutil to achieve the purpose of file renaming.
import shutilshutil.move(‘11.txt‘,‘1.txt‘)
To modify a file's suffix name:
import osfiles = os.listdir(‘.‘)for filename in files:li = os.path.splitext(filename)#返回后文件名和后缀名的列表if li[1] == ‘.html‘: newname = li[0] + ‘.htm‘ os.rename(filename,newname)
The Glob module is used to match the path, returning a list of files that match the given criteria. The main function of the Glob module is Glob (), which returns multiple files that meet the same matching criteria. The above rendering needs to determine whether it is an HTML suffix, or you can use the Glob () function to directly match the file name. The matching code is as follows:
glob.glob(‘*.html‘)
The glob can also make a more matching match to the path. For example, match all text files in a directory that starts with W in the C drive.
1.7 Search and replace of files
Search and replace of file contents can be implemented using the find and replace of strings. For example, look for the string ' hello ' in the Htllo.txt file and count the number of occurrences of ' hello '. The code is as follows:
File handling of the Advanced Python path