A practical guide to Python file handling

The operation of files is a basic and important knowledge point in Python. Whether it is crawling, data analysis, web development, or writing graphical interfaces, or data analysis, file-related operations may be required.

Today, I will study and review in summary, Python various file processing.

One, open() mode

open() function

The open (file name, operation mode) function is used to open a file with a given file name in a specified mode.

Basic mode of file operation

Column name column name
w Write mode: the content of the file will be erased and rewritten
r Read mode: only read the content of the file
a Append mode: append content at the end of the file content

Example demonstration

Use w mode to write file content

Use r mode to read file content

Use a mode to append file content

Second, get the directory list

Basic directory list get function

Via os module

Via pathlib module

Example demonstration

The folder structure is as follows:

**Use os.listdir() to get **

**Use os.scandir() to get **

**Use pathlib to get **

Three, get file attributes

There are a wealth of functions and methods in Python to obtain file information.

Via os module

Via pathlib module

Example demonstration

Use os.stat()

The returned stat_result() object contains the following common information:

Use os.scandir()

Fourth, create a directory

You can still use the os module and the pathlib module to create a directory.

Via os module

Via pathlib module

Example demonstration

Five, delete files and directories

Delete Files

Via os module

Via pathlib module

Delete directory

Delete a single directory

Delete directory tree

Example demonstration

Delete Files

Delete directory

Six, file name pattern matching

Sometimes it is necessary to perform fuzzy query on file name pattern matching (including regular), and it is necessary to use functions and methods related to file name pattern matching.

Example demonstration

String processing

Use fnmatch

Seven, traverse the directory tree and process files

Example demonstration

8. Temporary files and directories

Example demonstration

Nine, copy, move and rename files

Copy

Move and rename

Ten. Archiving and Compression

Example demonstration

11. Read multiple files

Recommended Posts

A practical guide to Python file handling
A complete guide to Python web development
How to write python configuration file
How to make a globe with Python
A quick introduction to Python regular expressions
How to sort a dictionary in python
Python file operation
Python3 development environment to build a detailed tutorial
How to simulate gravity in a Python game
01. Introduction to Python
python_ file processing
python-Use python to write a small shopping program
How to switch the hosts file using python
How to write a confession program in python
Python exception handling
How to deal with python file reading failure
Introduction to Python
How to practice after the python file is written
How to understand a list of numbers in python
How to create a Python virtual environment in Ubuntu 14.04
Python implements FTP to upload files in a loop
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python practical quantitative trading
Python error handling method
How to find the area of a circle in python