Search code examples
pythonglob

is python glob.glob '\' robust across different OS win/unix/mac?


I have the following code:

import glob
files = ".\database\*.txt"
for pathname in glob.glob(files):
    print pathname

I understand in different OS directory are represented differently. So I am wondering is ".\database*.txt" robust across different OS?

I tried

files = "./database/*.txt"

and it also worked.

If I want to make my code robust, what is the best way to do it?


Solution

  • You can ask Python what to use:

    In [1]: import os.path
    
    In [2]: os.path.sep
    Out[2]: '/'