


python3 method to traverse and delete files with specific suffix names
The following is a python3 method for traversing and deleting files with specific suffix names. It has a good reference value and I hope it will be helpful to everyone. Let's come and take a look
The U disk is poisoned. There is an extra .lnk file in each folder in the U disk. Virgo has done it again. I really can't bear it, so I wrote a script to save all the files. The .lnk file was deleted.
Multi-level directory recursive deletion
import os n = 0 for root, dirs, files in os.walk('./'): for name in files: if(name.endswith(".lnk")): n += 1 print(n) os.remove(os.path.join(root, name))
Save this script as rm.py , then put it in the root directory of the U disk, cd into the root directory of the U disk, and then:
python rm.py
to delete all the files in the U disk. lnk file, including subfolders.
Here you only need to specify the parameters of os.walk(), ./ is the current directory, so that the traversal can traverse all directories and files under the specified path, including multi-level directories. .
To be honest, after finally seeing more than 20 lnk files deleted, I felt extremely satisfied!
Delete the specified file in the current directory
import os n = 0 for root, dirs, files in os.walk('.'): for name in files: if("微信截图"in name): n += 1 print(n) print(name) os.remove(os.path.join(root, name))
The above code will delete the current folder Download all files whose file names contain "WeChat screenshots".
Related recommendations:
How to operate Python to traverse numpy arrays
##
The above is the detailed content of python3 method to traverse and delete files with specific suffix names. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to open files with the suffix .vsd In daily computer use, we often encounter various types of file formats, and one of the common file formats is files with the suffix .vsd. So, how should we open this file with the suffix .vsd? First, we need to understand the meaning and purpose of .vsd files. VSD is the file format of Visio, a graphical drawing tool developed by Microsoft. VSD files are mainly used to create and edit various types of diagrams

The document suffix, also called the file extension, is a mechanism used by the operating system to mark file types; the main function of the document suffix is to let the system decide what software should be used to run when the user opens the specified file, so it can be used Used to protect certain files, save disk space, clear icons on the settings panel, and set associations.

The OneDrive app on your system stores all your files and folders in the cloud. But sometimes users don't want certain files or folders to be stored and take up OneDrive space that is limited to 5 GB without a subscription. To do this, there is a setting in the OneDrive app that allows users to select files or folders to sync on the cloud. If you are also looking for this, then this article will help you select folders or files to sync in OneDrive on Windows 11. How to select certain folders to sync in OneDrive in Windows 11 Note: Make sure the OneDrive app is connected and synced

Discovering suffix factorials and corresponding suffix sums from arrays is entirely feasible when you understand the tools and techniques of the C++ programming language. That's exactly what we'll discuss in this article, including method syntax, algorithmic complexity, and efficient ways to unravel them. Additionally, this article shows two concrete code examples based on these methods. Finally, we summarize our insights into key takeaways. Syntax To ensure a clear understanding of the upcoming code examples, familiarize yourself with the syntax of the methods used before diving into their algorithms. //Methodsyntax<return_type>methodName(<parameters&

Files with the suffix eml are mail files, used to store email content and information. If you want to open an eml file, you can do it through the following methods. Method 1: Use email client software to install email client software, such as Microsoft Outlook, Mozilla Thunderbird, etc. Open the installed email client software. Find the "Import" or "Open" option in the software's menu bar or toolbar. Click the "Import" or "Open" option in the pop-up dialog box

Installation steps: 1. Make sure that Python3 has been installed and can be accessed through the command line; 2. Open the terminal and enter the "python3 -m ensurepip --upgrade" command to install pip; 3. Download the pip installation package from the official Python website; 4. Extract the downloaded pip installation package into a directory; 5. Open the terminal and navigate to the decompressed pip directory; 6. Run the "python3 setup.py install" command to install pip.

The path suffix refers to an identification symbol in a web page link or file path, usually in the form of a period "." and a suffix name, such as ".html", ".php", etc. In website development, sometimes we want to remove the suffix name in the path to achieve a more beautiful and friendly URL and improve search engine optimization. Below we will introduce how to use PHP to implement path suffix removal and provide specific code examples. First, we can use some string processing functions and regular expressions in PHP to implement the path

How to display hidden file suffixes: 1. Double-click to open the computer and find the tool bar; 2. Click [Folder Options] and [View] in sequence; 3. Uncheck the [Hide extensions for known file types] option. Can.
