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:
- 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): 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)
- 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. 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:
- #! /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]) deletes 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)
The above is our introduction to Python recursion.