简体   繁体   中英

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?

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 '.'

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 .

. [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. enter code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM