2

so.. this is my code :

import os

def line():
    pathInput = input("Type the addres of your file")
    pathExists = os.path.exists(pathInput)
    if(pathExists == true):
        something()
    elif(pathExists == false):
        print('said path is not avaialable')

def something():
    print("yet to work on it")

line()

my goal is that... it should continue on with the code only if the input path is available

i thought this would work but unfortunately it doesnt.... can anyone explain why it doesnt.. and what should the solution be ?

MADMENTAL
  • 33
  • 5
  • Do you get an error message? Does it say anything about `true` or `false` being undefined? These constants are capitalized in Python. Try `True` and `False` instead... and have a look at balderman's answer for the "pythonic" way. – MB-F Sep 25 '21 at 11:23

3 Answers3

2

I know it's get answered already but it's doesn't point out the problem. The fix is simple. What you are doing wrong is you are using small case true and false. But in python this is "True" and "False". As you can see first letter needs to capitalize.

import os

def line():
    pathInput = input("Type the addres of your file")
    pathExists = os.path.exists(pathInput)
    if(pathExists == True):
        something()
    elif(pathExists == False):
        print('said path is not avaialable')

def something():
    print("yet to work on it")

line()

But you can also do. Instead of writing like elif. And == True. You don't need to specify == True.

import os

def line():
    pathInput = input("Type the addres of your file")
    pathExists = os.path.exists(pathInput)
    if(pathExists):
        something()
    else:
        print('said path is not avaialable')

def something():
    print("yet to work on it")

line()
Akram Khan
  • 89
  • 2
1

You are looking for something like the below (no need for elif)

import os


def line():
    pathInput = input("Type the addres of your file")
    pathExists = os.path.exists(pathInput)
    if pathExists:
        something()
    else:
        print(f'said path ({pathInput}) is not avaialable')


def something():
    print("yet to work on it")


line()
balderman
  • 22,927
  • 7
  • 34
  • 52
1

The problem is in your boolean variable you have to use True instead of true and False instead of false as below.

import os

def line():
    pathInput = input("Type the addres of your file")
    pathExists = os.path.exists(pathInput)
    if pathExists:
        something()
    else:
        print('said path is not avaialable')

def something():
    print("yet to work on it")

line()

You don't necessarily need a check == True actually instead you can do it like above.