PYTHON pathinfo

is this article helpful?
|
Python replacement for PHP's pathinfo [ edit | history ]
import os.path
#__file__ is a variable representing the name of the file running.
absolute_path = os.path.abspath(__file__)
dirname = os.path.dirname(absolute_path)
basename = os.path.basename(absolute_path)
extension  = os.path.splitext(absolute_path)[-1]  # return ".py"
filename = __file__

print absolute_path
print dirname
print basename
print extension
print filename

PHP pathinfo

PHP original manual for pathinfo [ show | php.net ]

pathinfo

(PHP 4 >= 4.0.3, PHP 5)

pathinfoReturns information about a file path

Description

mixed pathinfo ( string $path [, int $options= PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )

pathinfo() returns an associative array containing information about path .

Parameters

path

The path being checked.

options

You can specify which elements are returned with optional parameter options . It composes from PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION and PATHINFO_FILENAME. It defaults to return all elements.

Return Values

The following associative array elements are returned: dirname, basename, extension (if any), and filename.

If options is used, this function will return a string if not all elements are requested.

Changelog

Version Description
5.2.0 The PATHINFO_FILENAME constant was added.

Examples

Example #1 pathinfo() Example

<?php
$path_parts 
pathinfo("/wiki//www/htdocs/index/");

echo 
$path_parts['dirname'], "\n";
echo 
$path_parts['basename'], "\n";
echo 
$path_parts['extension'], "\n";
echo 
$path_parts['filename'], "\n"// since PHP 5.2.0
?>

The above example will output:

/www/htdocs
index.html
html
index

Notes

Note: For information on retrieving the current path info, read the section on predefined reserved variables.

See Also

  • dirname() - Returns directory name component of path
  • basename() - Returns filename component of path
  • parse_url() - Parse a URL and return its components
  • realpath() - Returns canonicalized absolute pathname