There are two requirements:
Delete all. svn files in a directory and subdirectory
Delete all objects in a folder
In Python, file operations mainly come from the OS module. The main method is as follows:
OS. listdir (dirname): List directories and files under dirname
OS. getcwd (): Get the current working directory
OS. curdir: returns the current directory ('.')
OS. chdir (dirname): change the working directory to dirname
OS. path. isdir (name): determines whether the name is a directory. If the name is not a directory, false is returned.
OS. path. isfile (name): determines whether the name is a file. If the name does not exist, false is returned.
OS. path. exists (name): determines whether a file or directory name exists.
OS. path. getsize (name): Get the file size. If the name is a directory, return 0L
OS. path. abspath (name): Obtain the absolute path.
OS. path. normpath (path): standard path string format
OS. path. split (name): split the file name and directory (in fact, if you fully use the directory, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists)
OS. path. splitext (): separates file names and extensions
OS. path. join (path, name): Connection directory and file name or directory
OS. path. basename (path): returns the file name.
OS. path. dirname (path): Return file path
OS. remove (dir) # dir is the path of the folder or file to be deleted.
OS. rmdir (path) # path of the directory to be deleted. Note that the directory deleted using OS. rmdir must be an empty directory; otherwise, a function error occurs.
Delete the svn code in the directory:
Code
#! /Usr/bin/env python
# Coding = UTF-8
Import sys, OS, stat
Def walk (path ):
For item in OS. listdir (path ):
Subpath = OS. path. join (path, item)
Mode = OS. stat (subpath) [stat. ST_MODE]
If stat. S_ISDIR (mode ):
If item = ". svn ":
Print "Cleaning % s" % subpath
Print "% d deleted" % purge (subpath)
Else:
Walk (subpath)
Def purge (path ):
Count = 0
For item in OS. listdir (path ):
Subpath = OS. path. join (path, item)
Mode = OS. stat (subpath) [stat. ST_MODE]
If stat. S_ISDIR (mode ):
Count + = purge (subpath)
Else:
OS. chmod (subpath, stat. S_IREAD | stat. S_IWRITE)
OS. unlink (subpath)
Count + = 1
OS. rmdir (path)
Count + = 1
Return count
If len (sys. argv )! = 2:
Print "Usage: python cleansvn. py path"
Sys. exit (1)
Walk (sys. argv [1])
Delete all files and folders in a directory: Code
#! /Usr/bin/env python
# Coding = UTF-8
Import OS
Def delete_all_file (path ):
"Delete all folers and files"
If OS. path. isfile (path ):
Try:
OS. remove (path)
Except t:
Pass
Elif OS. path. isdir (path ):
For item in OS. listdir (path ):
Itemsrc = OS. path. join (path, item)
Delete_all_file (itemsrc)
Try:
OS. rmdir (path)
Except t:
Pass
If _ name _ = "_ main __":
Dirname = r'f: \ trunk'
Print delete_all_file (dirname)