# Python file and Path action function summary _python

Source: Internet
Author: User
Tags lstat
1:os.listdir (Path)//path as a directory
function is equivalent to executing the dir command in the path directory, returning to the list type
Print Os.listdir ('.. ')
2:os.path.walk (Path,visit,arg)
Path: Is the directory that will be traversed
Visit: Is a function pointer, the function circle is:
Callback (Arg,dir,filelist)
Where Arg is passed to walk Arg, dir is a directory under Path, filelist a list of files and directories under Dir, ARG: passed to visit
3:os.path.split (PATH)
Path is a route,
Output, divide the path into two parts, and look at the example:
Print Os.path.split ("Abc/de.txt")
(' abc ', ' De.txt ')
Os.path.split ("abc")
(", ' abc ')
Print Os.path.split ("De/abc/de")
(' de/abc ', ' de ')
4:os.path.splitext (filename)
Split file name into filename and extension
Os.path.splitext (Abc/abcd.txt)
(' ABC/ABCD ', ' txt ')
5:os.path.dirname (PATH)
Put the catalogue name forward.
Print Os.path.dirname ("abc")
#输出为空
Print os.path.dirname (' abc\def ')
Abc
6:os.path.basename (filename)
Get the main file name
Print Os.path.basename (' abc ')
Abc
Print os.path.basename (' abc.txt ')
Abc
Print os.path.basename (' bcd/abc ')
ABC #这个需要注意不包括目录名称
Print Os.path.basename ('. ')
.
7:os.mkdir (path, [mode])
Path is directory name: there is a requirement that only one level of directory can be created
For example, if path is abc/def, there must be ABC in the current directory or it will fail
8:os.makedirs (path [, mode])
You can create multilevel catalogs
9:os.remove (path) deletes a file, it must be a file
Os.removedirs (path) deletes everything in a directory
Os.rmdir (path) deletes a directory, and must be empty, otherwise os.errer
10:os.walk (PATH)
Traversing path, returning an object, each of his parts is a ternary group
(' Directory x ', [directory list under directory X], file under directory X
Example:
A = Os.walk ('. ')
For I in A:
Print I
11:shutil.copy (SRC,DST)
Copies the file SRC content to the file DST. , the target area must be writable, and if DST exists, DST is overwritten
――――――――――――――――――
11.1 Os.path--Normal path name operation
This module implements some functions that are useful on the path name.
Warning: Multiple of these functions in Windows do not correctly support UNC path names. Splitunc () and Ismount () can handle them correctly.
Abspath (PATH)
Returns a normalized, absolute version of the path pathname. On most platforms, it is equivalent to Normpath (Join (OS.GETCWD (), path). New features in the 1.5.2 version.
basename (PATH)
Returns the name at the bottom of the path path name. This is the second half of the pair returned by split (path). Note that the result of this function is different from the UNIX basename program; UNIX basename returns an empty string (') for '/foo/bar/' returning ' Bar ', basename () function.
Commonprefix (list)
Returns the longest path prefix (characters that take characters) and is a prefix for all paths in the list. If the list is empty, returns an empty string ('). Note You can return an invalid path because it operates one character at a time.
DirName (PATH)
Returns the directory name of the path path name. This is the first half of the pair returned by split (path).
Exists (PATH)
If path returns true if it is associated with an existing path. Returns False for a corrupted symbolic connection. On some platforms, if the permission is not granted to execute Os.stat on the requested file () This function may return false even if the path actually exists.
Lexists (PATH)
If path returns True if it is associated with an existing path. Returns True for a corrupted connection. On some platforms that lack os.lstat () are equivalent to exists (). New features in version 2.4.
Expanduser (PATH)
On UNIX, returns the argument "~" or "~user" as the beginning of the replacement user's home directory. The starting "~" is replaced by the environment variable home (if it is set); Otherwise, the current user's home directory is queried through the built-in module pwd in the password directory. The starting "~user" is queried directly in the password directory.
On Windows, only "~" is supported, which is replaced by the environment variable home or through a combination of homedrive and HomePath.
If the extension fails or if the path does not start with a wave number, the path returned does not change.
Expandvars (PATH)
Returns the parameters that are expanded using the environment variable. The substring of the "$name" or "${name}" format is replaced with the value of the environment variable name. Incomplete variable names and references to nonexistent variables are left unchanged.
Getatime (PATH)
Returns the last access time for path. The value returned is a number of seconds from the beginning of the new era (see Time module). Suspend Os.error If the file does not exist or cannot be accessed. New features in the 1.5.2 version. Change in version 2.3: if Os.stat_float_times () returns True, the result is a floating-point number.
Getmtime (PATH)
Returns the time when path was last modified. The return value is a number of seconds from the beginning of the new era (see Time module). Suspend Os.error If the file does not exist or cannot be accessed. New features in the 1.5.2 version. Change in version 2.3: if Os.stat_float_times () returns True, the result is a floating-point number.
Getctime (PATH)
Returns the CTime of the system, on some systems (like Unix), the last time it was changed, on other systems (like Windows), the time of path creation. The return value is a number of seconds from the beginning of the new era (see Time module). Suspend Os.error If the file does not exist or cannot be accessed. New features in version 2.3.
GetSize (PATH)
Returns the size of the path, in bytes. Suspend Os.error If the file does not exist or cannot be accessed. New features in the 1.5.2 version.
Isabs (PATH)
If path is an absolute pathname, returns True (starting with a slash).
Isfile (PATH)
Returns true if path is an existing normal file. This mimics the symbolic connection, so Islink () and Isfile () can return true for the same path.
Isdir (PATH)
Returns True if path is an existing directory. This mimics the symbolic link, so Islink () and Isdir () can return true for the same path.
If path refers to a directory entry, it is a symbolic connection, and returns True. Returns False If a symbolic connection is not supported.
Ismount (PATH)
If the path name path is a mount point returns True: At that point in the file system a different file system has been mounted. This function detects whether the path is a parent, path/..., and path is a different device, or is path/. and path point to the same i-node on the same device-this should be the detection of mount points for all Unix and POSIX variants.
Join (path1[, path2[, ...)]
Intelligently connects one or more path components. If any of the components are an absolute path, all the preceding components are discarded (on Windows, including the previous drive name, if any), and the connection continues. The returned value is a combination of path1 and optionally path2, correctly inserted between two components in a directory delimiter (OS.SEP), unless path2 is empty. Note on Windows, because each drive has a current directory, Os.path.join ("C:", "foo") represents a path (C:foo), rather than a c:\\foo, relative to the current directory on drive C:.
Normcase (PATH)
Standardizes the case of a path name. On UNIX, the unchanged path is returned, and on a case-insensitive file system, it converts the path to lowercase letters. On Windows, it also converts a forward slash to a backslash.
Normpath (PATH)
Standardize a path name. Collapses redundant (or verbose) delimiters and locates the outer references so that a//b,a/./b and a/foo/... /b all become A/b. It does not standardize case (the standardized case is written with Normcase ()). On Windows, it converts a forward slash to a backslash. If it contains symbolic connections, it should understand that this may change the intent of the path!
Realpath (PATH)
Returns the path to the specified file name standard, excluding any symbolic connections encountered in the path (if supported by the operating system). New features in version 2.2.
Samefile (path1, path2)
Returns True if the pathname parameter is associated with the same file or directory association (represented by device number and I-node number). If Os.stat () invokes each pathname, a failure hangs an exception. Effective: Macintosh,unix.
Sameopenfile (FP1, FP2)
Returns True if the file descriptor FP1 and FP2 are connected to the same file. Effective: Macintosh,unix.
Samestat (STAT1, STAT2)
Returns True if the stat tuple STAT1 and STAT2 are connected to the same file. This structure may be returned by Fstat (), Lstat (), or stat (). This function uses Samefile () and sameopenfile () to achieve a basic comparison. Effective: Macintosh,unix.
Split (PATH)
Split pathname path to a pair, (head, tail) tail is the last pathname component head is any thing that guides it. The tail section will not include slashes; if path ends with a slash, tail will be empty. If there is no slash in path, the head is empty. If path is empty, head and tail are empty. A trailing slash is removed from the head unless it is a root (one or more slashes only). In almost all cases, join (head, tail) is equivalent to path (only one exception is when the head is separated from the tail by multiple slashes).
Splitdrive (PATH)
Split path name path to a (drive, tail) pair, drive is a description of each drive or an empty string. Drive will always be a null character if the drive description is not used on the system. In all cases, the drive + tail will be the same as the path. New features in version 1.3.
Splitext (PATH)
Split pathname path to a pair (root, ext) so that root + ext = path, and ext is empty or starts with a period and contains only one period.
Splitunc (PATH)
Split pathname path to a pair (UNC, rest) to make it easy for UNC to be a UNC mount point (such as R ' \\host\mount ') if, and the rest of the rest path (such as R ' \path\file.ext '). The UNC will always be a null character for the path containing the drive name. Valid: Windows.
Walk (path, visit, ARG)
For each directory in the directory tree that is rooted in path (including path itself, if it is a directory), call function visit with arguments (ARG, dirname, names). The parameter dirname specifies the directory to access, and the parameters names lists the files (obtained from Os.listdir (dirname) in the directory). The visit function can modify the settings of the directory accessed under DirName, for example, to avoid accessing a part of the tree names. (objects that are connected by names must be modified in the appropriate location and assigned using Del or slice.) )
Note: Symbols connected to directories are not processed as a subdirectory, and therefore walk () will not access them. Access to connected directories you must identify them with Os.path.islink (file) and Os.path.isdir (file), and you must call walk ().
Related Keywords:
Related Article

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.