Tony
Tony

Reputation: 241

What's the difference between: os.listdir('.') vs os.listdir()

For the os library what is the difference between

os.listdir('.') vs os.listdir()

They both seem to produce the same results (a list of everything in the active directory) however:

https://www.tutorialspoint.com/python/os_listdir.htm

says that os.listdir specifically excludes '.' and '..' even if they are present in the directory. What does that mean?

Upvotes: 1

Views: 2946

Answers (3)

Mahesh Karia
Mahesh Karia

Reputation: 2055

.[dot] in listdir() refers to current directory & when we do not provide any input to listdir() then by default it lists current directory that's the reason it shows same result.

Upvotes: 0

Luke Smith
Luke Smith

Reputation: 967

There is no functional difference, see the docs. The definition of os.listdir() looks like this

os.listdir(path='.')

So the default value for path when you call os.listdir() is '.'

Upvotes: 3

janos
janos

Reputation: 124714

From help os.listdir:

listdir(path=None)
    Return a list containing the names of the files in the directory.

    path can be specified as either str or bytes.  If path is bytes,
      the filenames returned will also be bytes; in all other circumstances
      the filenames returned will be str.
    If path is None, uses the path='.'.

That is, os.listdir() is the same as os.listdir('.').

[...] says that os.listdir specifically excludes '.' and '..' even if they are present in the directory. What does that mean?

That concerns the returned values. In UNIX filesystems, every directory has . and .. entries, where . refers to the current directory, and .. to the parent directory. The documentation says that these entries will not be included in the list returned by os.listdir.

Upvotes: 2

Related Questions