mercredi 25 janvier 2017

IF something is True

I'm attempting to write some simple code that will check to see if a particular directory exists AND if that directory contains any files. If it exists AND it contains files, then I want it to do something. If not, then I want it to do something else.

I've been using the os module to perform the file explorer operations and thus far, it seems to be working just fine.

if os.listdir("D:\Inventory\Joy\XL") != []:  #Checks to see if the directory is empty
    print("This directory is full")
else:
    print("This directory is empty")


if print(os.path.exists("D:\Inventory\Joy\XL")) is True: #THIS ISN'T RIGHT BUT I DON'T KNOW WHAT TO DO.
    print("This directory exists")
else:
    print("The directory doesn't exist")

I've read that one should avoid using "if x == True then y" or "if x is True then y", but I don't know how (or why) to avoid it. Also, I woudn't know how to implement it even if I wanted to flout convention. People say it's redundant but, again, I don't understand why. I've read similar entries in Stack Overflow about this problem but I haven't understood any of the answers.

Aucun commentaire:

Enregistrer un commentaire