Common functions in OS. Path

Source: Internet
Author: User
Document directory
  • OS. Path. abspath (relative_path)
  • OS. Path. relpath (path [, start])
  • OS. Path. basename (PATH)
  • OS. Path. dirname (PATH)
  • OS. Path. Split (PATH)
  • OS. Path. Join (path1 [, path2 [,...])
  • OS. Path. normpath (PATH)
  • OS. Path. exists (PATH)
  • OS. Path. Walk (path, callback, ARG)
OS. Path. abspath (relative_path)

Returns the absolute path.

Abspath = normpath (join (OS. getcwd (), relative_path ))

Sample:

The current path is/home/justtest, then:

abspath('./code/test.py') => '/home/justtest/code/test.py
OS. Path. relpath (path [, start])

Returns the relative path. The default value of start IS OS. curdir.

Assume that the current path is/home/justtest.

relpath('/home/justtest/test.py') => 'test.py'relpath('/home/anothertest/test.py', '/home/anothertest') => 'test.py'
OS. Path. basename (PATH)

Returned file name

Basname (PATH) = Split (PATH) [1]

basename('/home/justtest/test.py') =>'test.py'basename('/home/justtest/') =>''
OS. Path. dirname (PATH)

The returned directory name, excluding the file name. Note: The returned path name does not contain the last slash.

Dirname (PATH) = Split (PATH) [0]

dirname('/home/justtest/test.py') =>/home/justtest'dirname('/home/justtest/') =>'/home/justtest'
OS. Path. Split (PATH)

Break the path into (path, file name)

split('/home/justtest/test.py') = ('/home/justtest', 'test.py')split('/home/justtest/') = ('/home/justtest', '')
OS. Path. Join (path1 [, path2 [,...])

Merge multiple paths

join('/home', 'justtest', 'test.py') => '/home/justtest/test.py'join('/home/justtest', 'test.py') => '/home/justtest/test.py'
OS. Path. normpath (PATH)

Normalize paths: Remove unnecessary delimiters, convert "..." to "True paths", and handle incorrect slashes.

normpath('\home/justtest') => '\\home/justtest'normpath('/home/./justtest') => '/home/justtest'normpath('/home/../justtest') => '/justtest'
OS. Path. exists (PATH)

Whether the file or path exists and has the permission to access it

OS. Path. isabs (PATH), isfile (PATH), isdir (PATH), islink (PATH)

Isabs: whether the path is absolute

Isfile: whether to file

Isdir: path?

Islink: whether to link

OS. Path. Walk (path, callback, ARG)

Traverses the path and calls the callback function for each file in the path.

The following is a prototype of the callback function:

Callback (ARG, path, files) @ Arg: parameter of the walk function @ path: path @ files: All files in the path

The following code prints all file names in the current directory:

from os.path import *def walk_callback(arg, path, files):for file in files:print join(path, file), argwalk('.', walk_callback, 'hello from walk')
 

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.