user773737
user773737

Reputation:

How do I write filepaths correctly in Python

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?

Upvotes: 0

Views: 204

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318558

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.

Upvotes: 5

Related Questions