Recursive processing of files and folders in the Python Module

Source: Internet
Author: User

There are two requirements:

  1. Delete all. svn files in a directory and subdirectory

  2. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.