简体   繁体   中英

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?

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.

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