有没有办法使用 os 库获取当前操作系统的目录分隔符?

There is a way to get the directories separator of the current operating system using the os library?

我只需要提取探索目录树的文件名。目前我计算目录字符分隔符的数量,这样我可以获得计算分隔符之前的字符的文件名。现在我在 windows 上,但代码也必须在 Linux 中 运行。目前我使用这个条件:

if file[n] == '\':
   #do something

但是在Linux这个条件应该是

if file[n] == '/':
   #do something

所以我问是否有一个函数可以为我提供当前 OS 的目录分隔符,例如:

if file[n] == os.directoriesSeparator():
   #do something

我查看了 os 库的文档,但没有找到这种功能。

路径分隔符可通过使用 os.path.sep 获得。例如:

import os

if file[n] == os.path.sep:
    # do something

也可以通过 os.sep

  import os
  for root, dir, file in os.walk('Give Directory Path'):
     print file