Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does python have os.path.curdir

Tags:

python

os.path.curdir returns '.' which is totally truthful and totally worthless. To get anything useful from it, you have to wrap it with os.path.abspath(os.path.curdir)

Why include a useless variable in the os.path module? Why not have os.path.curdir be a function that does the os.path.abspath for you?

Is there some historic reason for os.path.curdir to exist?

Maybe useless is a bit harsh, but not very useful seems weak to describe this. enter image description here

like image 480
boatcoder Avatar asked Jan 24 '13 22:01

boatcoder


People also ask

What is os path Curdir in Python?

The constant string used by the operating system to refer to the current directory. This is '. ' for Windows and POSIX. Also available via os.

What does os path dirname (__ file __) do?

path. dirname() method in Python is used to get the directory name from the specified path.

Why we use os path join?

Using os. path. join makes it obvious to other people reading your code that you are working with filepaths. People can quickly scan through the code and discover it's a filepath intrinsically.

What does os module do in Python?

The OS module in Python provides functions for creating and removing a directory (folder), fetching its contents, changing and identifying the current directory, etc. You first need to import the os module to interact with the underlying operating system.


3 Answers

It is a constant, just like os.path.sep.

Platforms other than POSIX and Windows could use a different value to denote the 'current directory'. On Risc OS it's @ for example, on the old Macintosh OS it's :.

The value is used throughout the standard library to remain platform agnostic.

Use os.getcwd() instead; os.path.abspath() uses that function under the hood to turn os.path.curdir into the current working directory anyway. Here is the POSIX implementation of abspath():

def abspath(path):
    """Return an absolute path."""
    if not isabs(path):
        if isinstance(path, _unicode):
            cwd = os.getcwdu()
        else:
            cwd = os.getcwd()
        path = join(cwd, path)
    return normpath(path)
like image 93
Martijn Pieters Avatar answered Oct 10 '22 19:10

Martijn Pieters


The value of os.path.curdir is "." on Linux, Windows, and OS X. It is, however, ":" on old Mac OS 9 systems. Python has been around long enough that this used to be important.

like image 36
Dietrich Epp Avatar answered Oct 10 '22 18:10

Dietrich Epp


It's just a constant, platform-dependent value. From the docs (which are worth reading):

The constant string used by the operating system to refer to the current directory. This is '.' for Windows and POSIX. Also available via os.path.

You might consider using os.getcwd() instead.

like image 8
cdhowie Avatar answered Oct 10 '22 20:10

cdhowie