473,287 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Retrieving Filename from Path

Hi,

Given a string representing the path to a file, what is the best way to
get at the filename? Does the OS module provide a function to parse the
path? or is it acceptable to split the string using '/' as delimiters
and get the last 'word'. The reason I'm not entirely happy with that
method is that it is platform specific. I would prefer to use a built
in method if possible.

Cheers,

Rob Cowie

Aug 31 '05 #1
2 23339
>>> import os.path
help(os.path)
Help on module ntpath:

NAME
ntpath - Common pathname manipulations, WindowsNT/95 version.

FILE
c:\data\utils\python24\lib\ntpath.py

DESCRIPTION
Instead of importing this module directly, import os and refer to
this
module as os.path.

FUNCTIONS
abspath(path)
Return the absolute version of a path

basename(p)
Returns the final component of a pathname

commonprefix(m)
Given a list of pathnames, returns the longest common leading
component

dirname(p)
Returns the directory component of a pathname

exists(path)
Test whether a path exists

expanduser(path)
Expand ~ and ~user constructs.

If user or $HOME is unknown, do nothing.

expandvars(path)
Expand shell variables of form $var and ${var}.

Unknown variables are left unchanged.

getatime(filename)
Return the last access time of a file, reported by os.stat()

getctime(filename)
Return the creation time of a file, reported by os.stat().

getmtime(filename)
Return the last modification time of a file, reported by
os.stat()

getsize(filename)
Return the size of a file, reported by os.stat()

isabs(s)
Test whether a path is absolute

isdir(path)
Test whether a path is a directory

isfile(path)
Test whether a path is a regular file

islink(path)
Test for symbolic link. On WindowsNT/95 always returns false

ismount(path)
Test whether a path is a mount point (defined as root of drive)

join(a, *p)
Join two or more pathname components, inserting "\" as needed

normcase(s)
Normalize case of pathname.

Makes all characters lowercase and all slashes into
backslashes.

normpath(path)
Normalize path, eliminating double slashes, etc.

realpath = abspath(path)
Return the absolute version of a path

split(p)
Split a pathname.

Return tuple (head, tail) where tail is everything after the
final slash.
Either part may be empty.

splitdrive(p)
Split a pathname into drive and path specifiers. Returns a
2-tuple
"(drive,path)"; either part may be empty

splitext(p)
Split the extension from a pathname.

Extension is everything from the last dot to the end.
Return (root, ext), either part may be empty.

splitunc(p)
Split a pathname into UNC mount point and relative path
specifiers.

Return a 2-tuple (unc, rest); either part may be empty.
If unc is not empty, it has the form '//host/mount' (or similar
using backslashes). unc+rest is always the input path.
Paths containing drive letters never have an UNC part.

walk(top, func, arg)
Directory tree walk with callback function.

For each directory in the directory tree rooted at top
(including top
itself, but excluding '.' and '..'), call func(arg, dirname,
fnames).
dirname is the name of the directory, and fnames a list of the
names of
the files and subdirectories in dirname (excluding '.' and
'..'). func
may modify the fnames list in-place (e.g. via del or slice
assignment),
and walk will only recurse into the subdirectories whose names
remain in
fnames; this can be used to implement a filter, or to impose a
specific
order of visiting. No semantics are defined for, or required
of, arg,
beyond that arg is always passed to func. It can be used,
e.g., to pass
a filename pattern, or a mutable object designed to accumulate
statistics. Passing None for arg is common.

DATA
__all__ = ['normcase', 'isabs', 'join', 'splitdrive', 'split',
'splite...
altsep = '/'
curdir = '.'
defpath = r'.;C:\bin'
devnull = 'nul'
extsep = '.'
pardir = '..'
pathsep = ';'
sep = r'\'
supports_unicode_filenames = True


Rob Cowie wrote: Hi,

Given a string representing the path to a file, what is the best way to
get at the filename? Does the OS module provide a function to parse the
path? or is it acceptable to split the string using '/' as delimiters
and get the last 'word'. The reason I'm not entirely happy with that
method is that it is platform specific. I would prefer to use a built
in method if possible.

Cheers,

Rob Cowie


Aug 31 '05 #2
Thanks,

os.path.basename(filePath) it is then.

BTW, the help(module) function is new to me! Must have missed it when
reading the tutorial.

Cheers!

Aug 31 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Don | last post by:
How do I retrieve the full path (C:\.....filename) of a file uploaded to a php script on the server. I think $_FILES will only provide 'name', which is only the filename itself. Thanks, Don ...
2
by: VM | last post by:
How can I retrieve the whole directory where my exe is located? I would've thought Directory.GetCurrentDirectory worked but any event that may change the dir (eg. OpenFileDialog or SaveFileDialog)...
2
by: Csharper95 | last post by:
I want to get the file name and path from a SaveFileDialog (for a 'save as' operation) after the user has saved the information under a new file name (and presumably under a new path) ? I want...
7
by: Dean McCarten | last post by:
I Need to Retrieve the UNC Path of Mapped Drives on a Client Machine's (NT4 + y2k) on my Network, but I have been unsuccessful so far and I find seem to find a way to do this, any idea anyone I...
1
by: Jon Pope | last post by:
I've got a web service which can be installed by a customer within a virtual folder whose name is defined by them during installation. For example, the path to the web service could be: ...
1
by: psbasha | last post by:
Hi, I would like to get the path of the given file name. Say I/P: C:\Sample\Sample1.txt
6
by: SpreadTooThin | last post by:
After I select a file with the OpenFileDialog, I get the file name with .FileName, but I want to know the drive and path where that file is. What function, method, class can I use in C# to get the...
2
by: =?Utf-8?B?SmFtZXM=?= | last post by:
Hi, I am aware of 256 characters path limit in naming a file, but can you help me how to limit the path for file naming to 240 characters only? is there a way to limit that in registry editor or is...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.