Python is an object-oriented interpretive computer programming language, but also a powerful and perfect universal language, has more than 10 years of development history, mature and stable. Python has the richest and most powerful class library in the scripting language, enough to support most everyday applications. It has a simple, easy to learn, free, open source, portability, interpretation, object-oriented, scalable, embeddable and Rich library features, the current scope of application is very wide, such as system programming, image processing, database programming and so on.
Python developers can write program code using a text editor, such as Windows Notepad, or a professional IDE (integrated development environment). The IDE makes it easy for developers to create, run, and debug Python programs. The IDE is available for download at the official Python website (http://www.python.org), and the current (September 2009) version is Python 3.1.1, which will be presented in Python 2.6.2 for the development platform.
About the Python program, in fact, a Python program is equivalent to an application, it does not need to be compiled, only the user computer installed on the Python environment can be. To run a PY program, double-click the py file directly. In general, without prompting the user to enter or control the screen display, open a py file will suddenly flash out immediately, this is because the program has been completed. If you want to display, add a screen pause code:
Os.system (' pause ')
Before using this code, you need to refer to the OS module: Import os
Here's how to start the Python program for deleting files:
Many software in the runtime will automatically create some backup files, after the program quit and do not automatically delete the backup files, as the number of files increase, every once in a while to clean up. If the number of files is larger, manual cleanup is obviously more cumbersome. You can then write a Python script to complete the task. The following code:
Copy Code code as follows:
#-*-coding:cp936-*-
#file: e01.py
Import OS
#该函数用于删除文件
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 "Done!"
#提示用户输入目录路径
Path=raw_input ("Please enter the directory where you want to delete the file: (such as D:\\temp)")
If os.path.exists (path) ==false: #检查用户输入的目录是否存在, exiting the program if it does not exist
The directory entered by print does not exist! "
Os._exit (1)
Os.path.walk (path, scan, 0)
Os.system (' pause ')
Runs the program, which deletes files in the user-specified directory at the beginning of the tilde (~) or at the end of the suffix (. bak). The results of the operation are shown in the following illustration:
Here's how to parse this code. First, system operations are in the OS model, all of which need to import the OS model first. The user is then prompted to enter the file directory, while checking that the file directory entered by the user is correct. Verify that the directory exists using the Os.path.exists (path) method, or if return true indicates that the directory exists, and if False indicates that it does not exist, exit the program. Exit the Python program using the Os._exit (1) method. The Os.path.walk () method is to access each directory and file in the directory, calling function scan within that method. The parameter of the function scan is 3, where names represents the names of all the files within the directory, and is the list type. Then, for each file, check that the file name matches the feature that the file name is to be deleted (the one that ends with a wavy sign (~) or a suffix (. bak)) and, if it meets the requirements, is deleted using the Os.remove (file) method. It is noteworthy here that using the Os.remove (file) method to delete a file requires that the parameter file be a full path and file name, such as D:\temp\1.bak.
If you want to delete the TMP temporary file, simply change the "file[-4:]==". Bak "" In the previous code to file[-4:]==. tmp "". The last sentence (Os.system (' pause ')) indicates that the screen is paused.