Use Python recursion to process objects

Source: Internet
Author: User

Among the many Python application techniques, file operations related methods are an important application technology. Here we will introduce Python recursion to understand its role in file operations, hoping to help you.

  • Rules for correct use of Python Unit Tests
  • Use PDB for Python program debugging
  • Basic Application Syntax of Python constructor list
  • Share basic operations for implementing INI File Operations in Python
  • How to Implement tab file operations in Python

Python recursion has two requirements:

Delete all. svn files in a directory and subdirectory

Delete all objects in a folder

In Python, file operations in Python recursion mainly come from the OS module. The main method is as follows:

  1. OS. listdir (dirname): List directories and files under dirname
  2. OS. getcwd (): Get the current working directory
  3. OS. curdir: returns the current directory '.')
  4. OS. chdir (dirname): change the working directory to dirname

  1. OS. path. isdir (name): determines whether the name is a directory. If the name is not a directory, false is returned.
  2. OS. path. isfile (name): determines whether the name is a file. If the name does not exist, false is returned.
  3. OS. path. exists (name): determines whether a file or directory name exists.
  4. OS. path. getsize (name): Get the file size. If the name is a directory, return 0L
  1. OS. path. abspath (name): Obtain the absolute path.
  2. OS. path. normpath (path): standard path string format
  3. OS. path. split (name): separates file names and directories.
    In fact, if you fully use a directory, it will also separate the last directory as a file name,
    At the same time, it does not determine whether a file or directory exists)
  4. OS. path. splitext (): separates file names and extensions
  5. OS. path. join (path, name): Connection directory and file name or directory
  6. OS. path. basename (path): returns the file name.
  7. OS. path. dirname (path): Return file path
  1. OS. remove (dir) # dir is the path of the folder or file to be deleted.
  2. OS. rmdir (path) # path of the directory to be deleted. It must be noted that,
    The directory deleted using OS. rmdir must be empty; otherwise, the function fails.

Python recursively deletes the svn code in the directory:

 
 
  1. #! /Usr/bin/env python
  2. # Coding = UTF-8
  3. Import sys, OS, stat
  4. Def walk (path ):
  5. For item in OS. listdir (path ):
  6. Subpath = OS. path. join (path, item)
  7. Mode = OS. stat (subpath) [stat. ST_MODE]
  8. If stat. S_ISDIR (mode ):
  9. If item = ". svn ":
  10. Print "Cleaning % s" % subpath
  11. Print "% d deleted" % purge (subpath)
  12. Else:
  13. Walk (subpath)
  14. Def purge (path ):
  15. Count = 0
  16. For item in OS. listdir (path ):
  17. Subpath = OS. path. join (path, item)
  18. Mode = OS. stat (subpath) [stat. ST_MODE]
  19. If stat. S_ISDIR (mode ):
  20. Count + = purge (subpath)
  21. Else:
  22. OS. chmod (subpath, stat. S_IREAD | stat. S_IWRITE)
  23. OS. unlink (subpath)
  24. Count + = 1
  25. OS. rmdir (path)
  26. Count + = 1
  27. Return count
  28. If len (sys. argv )! = 2:
  29. Print "Usage: python cleansvn. py path"
  30. Sys. exit (1)
  31. Walk (sys. argv [1]) deletes all files and folders in a directory:
  32. Code
  33. #! /Usr/bin/env python
  34. # Coding = UTF-8
  35. Import OS
  36. Def delete_all_file (path ):
  37. "Delete all folers and files"
  38. If OS. path. isfile (path ):
  39. Try:
  40. OS. remove (path)
  41. Except t:
  42. Pass
  43. Elif OS. path. isdir (path ):
  44. For item in OS. listdir (path ):
  45. Itemsrc = OS. path. join (path, item)
  46. Delete_all_file (itemsrc)
  47. Try:
  48. OS. rmdir (path)
  49. Except t:
  50. Pass
  51. If _ name _ = "_ main __":
  52. Dirname = r'f: \ trunk'
  53. Print delete_all_file (dirname)

The above is our introduction to Python recursion.

Related Article

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.