Jay Junior
Jay Junior

Reputation: 55

Os.path gives unexpected output

lately I started working with the Os module in python . And I finally arrived to this Os.path method . So here is my question . I ran this method in one of my kivy project just for testing and it actually didn't returned the correct output.The method consisted of finding if any directory exist and return a list of folders in the directory . otherwise print Invalid Path and return -1 . I passed in an existing directory and it returned -1 but the weird path is that when I run similar program out of my kivy project using the same path present in thesame folder as my python file it return the desired output .here is the image with the python file and the directory name image I have tested which returns invalid path.

enter image description here and here is my code snippet

    def get_imgs(self, img_path):
        if not os.path.exists(img_path):
            print("Invalid Path...")
            return -1
        else:
            all_files = os.listdir(img_path)
            imgs = []
            for f in all_files:
                if (
                f.endswith(".png")
                or f.endswith(".PNG")
                or f.endswith(".jpg")
                or f.endswith(".JPG")
                or f.endswith(".jpeg")
                or f.endswith(".JPEG")
            ):
                imgs.append("/".join([img_path, f]))
        return imgs

Upvotes: 0

Views: 160

Answers (1)

Pete
Pete

Reputation: 121

It's tough to tell without seeing the code with your function call. Whatever argument you're passing must not be a valid path. I use the os module regularly and have slowly learned a lot of useful methods. I always print out paths that I'm reading or where I'm writing before doing it in case anything unexpected happens, I can see that img_path variable, for example. Copy and paste the path in file explorer up to the directory and make sure that's all good.

Some other useful os.path methods you will find useful, based on your code:

os.join(<directory>, <file_name.ext>) is much more intuitive than imgs.append("/".join([img_path, f]))

os.getcwd() gets your working directory (which I print at the start of scripts in dev to quickly address issues before debugging). I typically use full paths to play it safe because Python pathing can cause differences/issues when running from cmd vs. PyCharm

os.path.basename(f) gives you the file, while os.path.dirname(f) gives you the directory.

It seems like a better approach to this is to use pathlib and glob. You can iterate over directories and use wild cards. Look at these:

iterating over directories: How can I iterate over files in a given directory?

different file types: Python glob multiple filetypes

Then you don't even need to check whether os.path.exists(img_path) because this will read the files directly from your file system. There's also more wild cards in the glob library such as * for anything/any length, ? for any character, [0-9] for any number, found here: https://docs.python.org/3/library/glob.html

Upvotes: 1

Related Questions