

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove swap files using Python?
Swap files have the extension .swp. Easiest way to remove all swap files from a folder recursively is to use the string function endswith with the extension name(.swp) to match file names and delete these files.
example
import os, os.path mypath = "my_folder" for root, dirs, files in os.walk(mypath): for file in filter(lambda x: x.endswith('.swp'), files): os.remove(os.path.join(root, file))
This program will recursively search the directory, "my_folder" and delete all files that end with .swp.
- Related Questions & Answers
- How to remove hidden files and folders using Python?
- How to Swap Two Variables using Python?
- How to convert PDF files to Excel files using Python?
- How to create powerpoint files using Python
- How to rename multiple files recursively using Python?
- How to remove a directory using Python?
- How to remove a file using Python?
- How to close all the opened files using Python?
- How to touch all the files recursively using Python?
- How to ignore hidden files using os.listdir() in Python?
- How to read text files using LINECACHE in Python
- Rename multiple files using Python
- How to copy files to a new directory using Python?
- How to list down all the files alphabetically using Python?
- How are files added to a tar file using Python?
Advertisements