Python中删除文件的程序代码
Python是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定。Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用。它具有简单、易学、免费、开源、可移植性、解释性、面向对象、可扩展性、可嵌入性以及丰富的库等特性,目前的应用范围也非常广泛,如系统编程、图像处理、数据库编程等方面。
Python开发者可以使用文本编辑器(如Windows的记事本等)或者专业的IDE(集成开发环境)来编写程序代码。IDE使得开发者可以很方便地创建、运行、调试Python程序。IDE可以在Python的官方网站(http://www.python.org)下载,目前(2009年9月)最新版本为Python 3.1.1,本文中将以Python 2.6.2为开发平台进行介绍。
关于Python程序的运行,其实一个Python程序就相当于一个应用程序,它不需要经过编译,只需要用户电脑上面安装Python环境即可。要运行一个py程序,直接双击这个py文件即可。一般情况下,没有提示用户输入或控制屏幕显示,打开一个py文件时会突然闪一下马上就退出,这是由于程序运行已经完成了。若需要显示,则要添加一个屏幕暂停的代码:
os.system('pause')
在使用这个代码前,需要引用os模块:import os
下面开始介绍删除文件的Python程序设计:
很多软件在运行时会自动创建一些备份文件,在程序退出后又不自动删除备份文件,随着文件数量的增加,每隔一段时间就要清理一下。如果文件数量比较多,手工清理显然比较麻烦。于是可以编写一个Python脚本来完成该任务。如下代码:
代码如下:
# -*- coding: cp936 -*-
#file:E01.py
import os
#该函数用于删除文件
def scan(arg, dirname, names):
for file in names:
if file[0]=="~" or file[-4:]==".bak":
print "删除文件:", file
file=dirname+"\\"+file
os.remove(file)
print "完成!"
#提示用户输入目录路径
path=raw_input("请输入要删除文件所在目录:(如D:\\temp)")
if os.path.exists(path)==False: #检查用户输入的目录是否存在,如果不存在则退出程序
print "输入的目录不存在!"
os._exit(1)
os.path.walk(path, scan, 0)
os.system('pause')
运行该程序,它将删除用户指定目录下的以波浪符号(~)开头或以后缀(.bak)结尾的文件。运行结果如下图所示:
下面来分析这段代码。首先,系统操作都在os模型中,所有首先需要导入os模型。然后提示用户输入文件目录,同时检查用户输入的文件目录是否正确。检验目录是否存在使用os.path.exists(path)方法,如果返回True则表示目录存在,如果返回False则表示不存在,则退出程序。退出Python程序采用os._exit(1)方法。os.path.walk()方法是访问目录中的每个目录以及文件,在该方法内调用函数scan。函数scan的参数指定为3个,其中names表示目录内所有文件的名称,为列表类型。然后对于每一个文件检查其文件名是否符合要删除文件名的特征(以波浪符号(~)开头或以后缀(.bak)结尾的文件),如果符合要求,则采用os.remove(file)方法删除。在此值得注意的是,采用os.remove(file)方法删除文件,要求参数file为全路径和文件名,如D:\temp\1.bak。
如果要删除tmp临时文件,只需要将上段代码中的“file[-4:]==".bak"”更改为“file[-4:]==".tmp"”即可。最后一句(os.system('pause'))表示屏幕暂停。

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

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
