Use python os module provides for file and directory operation, rename files, add, delete, copy directories and files.
First, the file directory of commonly used functions
During file and directory operations, we will use the following general operations.
1, the current; path
Os.getcwd may be used in python () function to get the current path.
os.getcwd() '''帮助文档:Return a unicode string representing the current working directory.'''
This function does not need to pass arguments, it returns the current directory. It should be noted that the current directory is not the location of the script, but the script's directory operation.
Change the current directory using os.chdir script () function
os.chdir() '''帮助文档:chdir(path) Change the current working directory to the specified path. path may always be specified as a string. On some platforms, path may also be specified as an open file descriptor. If this functionality is unavailable, using it raises an exception.'''
2, access to the content directory
You can use os.listdir in python () function to get the contents of the specified directory.
os.listdir(path) ''' 帮助文档:listdir(path=None) Return a list containing the names of the files in the directory. path can be specified as either str, bytes, or a path-like object. If path is bytes, the filenames returned will also be bytes; in all other circumstances the filenames returned will be str. If path is None, uses the path='.'. On some platforms, path may also be specified as an open file descriptor;\ the file descriptor must refer to a directory. If this functionality is unavailable, using it raises NotImplementedError. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.'''
To get the contents of a directory path: path parameters
3, create a directory
You can use os.mkdir in python () function to create the directory.
os.mkdir(path) '''帮助文档mkdir(path, mode=511, *, dir_fd=None) Create a directory. If dir_fd is not None, it should be a file descriptor open to a directory, and path should be relative; path will then be relative to that directory. dir_fd may not be implemented on your platform. If it is unavailable, using it will raise a NotImplementedError. The mode argument is ignored on Windows.'''
path parameters: To create a path to the directory
os.mkdir () function can only create a directory, that directory path parameters in addition to the final surface to be created does not exist, all directory paths must exist before otherwise there will be mistakes, and when this will occur directory exists error "when the file already exists, the file can not be created."
For this purpose we can use another function os.makedirs () function to create a multi-level empty directory
os.makedirs(path) '''帮助文档:makedirs(name, mode=511, exist_ok=False) makedirs(name [, mode=0o777][, exist_ok=False]) Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. If the target directory already exists, raise an OSError if exist_ok is False. Otherwise no exception is raised. This is recursive.'''
path parameters: To create a multi-stage empty directory, the last directory must not exist, or an error "when the file already exists, you can not create it."
4, delete the directory
You can use os.rmdir in python () function to delete directories
os.rmdir(path) '''帮助文档:rmdir(path, *, dir_fd=None) Remove a directory. If dir_fd is not None, it should be a file descriptor open to a directory, and path should be relative; path will then be relative to that directory. dir_fd may not be implemented on your platform. If it is unavailable, using it will raise a NotImplementedError.'''
path parameters: the path to the directory you want to delete, to delete the directory must be empty directory or an error "directory is not empty."
For this purpose we can use another function os.removedirs () to delete the empty multi-level directory, the directory must or will cause the error "The directory is not empty" is empty
5, to determine whether it is a directory
Os.path.isdir may be used in python () function to determine whether a directory path.
os.path.isdir(path) '''帮助文档:_isdir(path, /) Return true if the pathname refers to an existing directory.'''
Parameters path: the path to be determined
6, to determine whether the file
Os.path.isfile may be used in python () function to determine whether a file path.
os.path.isfile(path) '''帮助文档:isfile(path) Test whether a path is a regular file'''
Parameters path: the path to be determined
7 to determine whether the absolute path
Whether os.path.isabs () function to determine the path is an absolute path
os.path.isabs (path) '' 'help documentation: isabs (S) the Test Whether IS A path Absolute' ''
Parameters path: the path to be estimated
8, test path really exists
os.path.exists () function to determine whether it is a path exists
os.path.exists(path) '''帮助文档:exists(path) Test whether a path exists. Returns False for broken symbolic links'''
Parameters path: the path to be estimated
9, the separation path and file name
os.path.split(path) '''帮组文档:split(p) Split a pathname. Return tuple (head, tail) where tail is everything after the final slash. Either part may be empty.'''
Parameters path: the path to be isolated
10, separating the file extension
os.path.splitext(path) '''帮助文档:splitext(p) Split the extension from a pathname. Extension is everything from the last dot to the end, ignoring leading dots. Returns "(root, ext)"; ext may be empty.'''
Parameters path: the path to be isolated
11, get pathname
os.path.dirname (filename) Gets a path name only through the path of the file
os.path.dirname(filename) '''帮助文档:dirname(p) Returns the directory component of a pathname'''
Specific path to the file: path parameters
12, access to the file name
os.path.basename (filename) Gets the file name path through
os.path.basename(filename) '''帮助文档:basename(p) Returns the final component of a pathname'''
Specific path to the file: path parameters
13, read and set the environment variables
os.getenv () function to get the environment variables os.putenv () to set environment variables
os.getenv() '''帮助文档:getenv(key, default=None) Get an environment variable, return None if it doesn't exist. The optional second argument can specify an alternate default. key, default and the result are str.''' os.putenv() '''帮助文档:putenv(name, value, /)
Change or add an environment variable.'''
14, given the current line terminator of the platform
15, indicating the platform you are using
os.name () function specifies which platform you are currently using
windows platform for 'nt'
16, get the file attributes
os.stat (file) function to get the file attributes
os.stat() '''帮助文档:stat(path, *, dir_fd=None, follow_symlinks=True) Perform a stat system call on the given path. path Path to be examined; can be string, bytes, a path-like object or open-file-descriptor int. dir_fd If not None, it should be a file descriptor open to a directory, and path should be a relative string; path will then be relative to that directory. follow_symlinks If False, and the last element of the path is a symbolic link, stat will examine the symbolic link itself instead of the file the link points to. dir_fd and follow_symlinks may not be implemented on your platform. If they are unavailable, using them will raise a NotImplementedError.'''
17, rename the file or directory
os.rename (old, new) function to rename a file or directory
os.rename(old, new) '''帮助文档:rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None) Rename a file or directory. If either src_dir_fd or dst_dir_fd is not None, it should be a file descriptor open to a directory, and the respective path string (src or dst) should be relative; the path will then be relative to that directory. src_dir_fd and dst_dir_fd, may not be implemented on your platform. If they are unavailable, using them will raise a NotImplementedError.'''
18, access to the file size
os.path.getsize() '''帮助文档:getsize(filename) Return the size of a file, reported by os.stat().'''