Program code in Python to delete files

  • 2020-04-02 09:27:09
  • OfStack

Python is a kind of object-oriented interpretive computer programming language, and it is also a powerful and perfect general-purpose language with more than a decade of development history, maturity and stability. Python has the richest and most powerful library of classes in the scripting language and is capable of supporting most everyday applications. It is simple, easy to learn, free, open source, portable, explanatory, object-oriented, extensible, embeddable and rich library features, the current application scope is also very wide, such as system programming, image processing, database programming and other aspects.
Python developers can write code using either a text editor (like notepad for Windows) or a professional IDE (integrated development environment). Ides make it easy for developers to create, run, and debug Python programs. The IDE can be downloaded from the official Python web site (http://www.python.org). The current (September 2009) version is Python 3.1.1, and this article USES Python 2.6.2 as the development platform.
As for the running of Python programs, a Python program is actually equivalent to an application program. It does not need to be compiled, just need to install the Python environment on the user's computer. To run a py program, just double-click on the py file. In general, there is no prompt for input or control of the screen display, and when a py file is opened, there is a sudden flash and it exits immediately, because the program has finished running. To display, add a screen pause code:
OS. The system (" pause ")
Before you can use this code, you need to refer to the OS module: import OS
Here's the beginning of the Python program for deleting files:
Many software programs automatically create backup files when they run, do not automatically delete the backup files after the program exits, and clean up every once in a while as the number of files increases. If the number of files is large, manual cleaning is obviously cumbersome. You can then write a Python script to do this. The following code:
 
# -*- coding: cp936 -*- 
#file:E01.py 
import os 
# This function is used to delete files  
def scan(arg, dirname, names): 
for file in names: 
if file[0]=="~" or file[-4:]==".bak": 
print " Delete file: ", file 
file=dirname+"\"+file 
os.remove(file) 
print " complete !" 
# The user is prompted for a directory path  
path=raw_input(" Please enter the directory where you want to delete the file: ( Such as D:\temp)") 
if os.path.exists(path)==False: # Check whether the directory entered by the user exists, and exit the program if it does not  
print " The directory you entered does not exist! " 
os._exit(1) 
os.path.walk(path, scan, 0) 
os.system('pause') 

Run the program, which deletes files in the user-specified directory that begin with a tilde (~) or end with the suffix (.bak). The results are shown in the following figure:
Let's examine this code. First, the system operations are all in the OS model, so you need to import the OS model first. The user is then prompted for a file directory and checked to see if the file directory entered by the user is correct. Verify the existence of a directory using the os.path. Exists (path) method, returning True if the directory exists and False if it does not, and exiting the program. Exit the Python program using the os._exit(1) method. The os.path.walk() method accesses each directory and file in the directory, within which the function scan is called. The function scan takes three arguments, where names represent the names of all the files in the directory, of the list type. Then, for each file, check to see if the file name matches the character of the file name to be deleted (the file that begins with the tilde (~) or ends with the suffix (.bak)), and if so, use the os.remove(file) method. It is worth noting here that the os.remove(file) method is used to delete the file, requiring the parameter file to be the full path and file name, such as D:\temp\1.bak.
If you want to delete the TMP temporary files, only need to code in the "file / - 4: = =" bak "" changed to" file / - 4: = = ". TMP "". The last sentence (os.system('pause')) indicates a pause on the screen.

Related articles: