0

I'm trying to find all the files in a directory.

import glob
import os
os.chdir("C:\test\\")
for files in glob.glob("*.*"):
    print(files)

But this returns nothing, even though there are files in C:\test\

So... what's going on, and how do I fix this?

  • Duplicate of this http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python – Aleksander Lidtke Oct 02 '13 at 21:10
  • 1
    `\t` is a tab character. That's why nothing matches. Forward slashes work fine on Windows (only a DOS box needs backward slashes). Or you can try using raw strings instead (like `r"C:\test"`) - but raw strings cannot end with an odd number of backslashes. – Tim Peters Oct 02 '13 at 21:13

1 Answers1

5

in "C:\test\\" the \t evaluates to a tab character. What you want is "C:/test/" or r"C:\test" - the difference is that the first version makes use of the fact that all windows apis and thus also python support forward slashes, too. The second one is a raw string where no escape sequences exist.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636