简体   繁体   中英

Avoid double backslash in windows path

my program should parse /classifiers folder and go throught some calc. Here is a paths for folders:

classifiers_path = os.path.join('C:/Users/feature/classifiers')

Here is a lines of code, that parses that folder:

        for i in os.path.join(classifiers_path + '/*.pkl'):
        # Pulling the model from the path
        model = joblib.load(i)

While debugging i have paths like this:

C:/Users/feature/classifiers\\T1.pkl

So it breaks with FileNotFoundError, How to avoid this double \\, and it should be written, for unix/windows? Thanks

As is stated in the comments, you should be iterating through the files in the directory, not through the string resulting from os.path.join() . So instead, iterate through the list of files in your classifiers_path directory resulting from passing it to os.listdir() like this:

for i in os.listdir(os.path.join(classifiers_path)):
        if i.endswith('.pkl'):
            model = joblib.load(i)

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