Python os.remove () method
Python OS file / directory methods
Outline
os.remove () method is used to delete the specified file path. If the specified path is a directory, it will throw OSError.
Valid in Unix, Windows in
grammar
remove () method syntax is as follows:
os.remove(path)
parameter
path - file path to be removed
return value
This method does not return value
Examples
The following example demonstrates the remove () method of use:
#!/usr/bin/python # -*- coding: UTF-8 -*- import os, sys # 列出目录 print "目录为: %s" %os.listdir(os.getcwd()) # 移除 os.remove("aa.txt") # 移除后列出目录 print "移除后 : %s" %os.listdir(os.getcwd())
The above program output is:
目录为: [ 'a1.txt','aa.txt','resume.doc' ] 移除后 : [ 'a1.txt','resume.doc' ]