#๐Ÿ”’ help

32 messages ยท Page 1 of 1 (latest)

worn gorge
#

can someone tell me how i would fix this

from pathlib import Path

def is_txt_file(path_to_file):
    t_or_f = 4
    file_path = Path(path_to_file)

    if file_path.suffix == ".txt":
        t_or_f = 1

    elif file_path.suffix is None:
        print(file_path.suffix)
        t_or_f = 2
      
    elif file_path.suffix is not None:
        print(file_path.suffix)
        t_or_f = 3
    else:
        print("error in code 1")

    return t_or_f

main_input = input("")

t_or_f = is_txt_file(Path(main_input))
if t_or_f == 1:
    print("file is txt")  

elif t_or_f == 2:
    print("file is value less")  

elif t_or_f == 3:
    print("error in code 2")

elif t_or_f == 4:
    print("error in code 3")
normal adderBOT
#

@worn gorge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

blazing crest
#

waht is the errorr

worn gorge
#

if the file type is wrong or not there it returns the same value of file_path.suffix is not None:

blazing crest
#

i think it works fine

worn gorge
#

then run it and put a path to a file thats a txt file and in the input put some file type or remove it

#

oh and the file doesnt even need to exist

blazing crest
#

letme rephrase what i think your code does

in main func:
you enter a file path

is_txt_file is called on the path:
if path ends with ".txt "
value is set 1

if path ends with nothing
value is 2

#

if path ends with something other than .txt
value is 3

worn gorge
#

yep

#

well i gtg just tell me how to fix it if you know ill look later thanks for the help

blazing crest
#

what to fix

#

what do you want changed

worn gorge
#

to make the code work

blazing crest
#

lmao

#

ok ill try

worn gorge
#

ok thanks

blazing crest
#
from pathlib import Path


def is_txt_file(path_to_file):
    t_or_f = 4
    file_path = Path(path_to_file)

    val = file_path.suffix

    if val == ".txt":
        t_or_f = 1

    elif val == '':
        print(file_path.suffix)
        t_or_f = 2

    elif len(val) > 0:
        print(file_path.suffix)
        t_or_f = 3
    else:
        print("error in code 1")

    return t_or_f


main_input = input("Input : ")

t_or_f = is_txt_file(Path(main_input))
if t_or_f == 1:
    print("file is txt")

elif t_or_f == 2:
    print("file is value less")

elif t_or_f == 3:
    print("error in code 2")

elif t_or_f == 4:
    print("error in code 3")

#

your code doesnt work because when you call .suffix method it returns an empty string when there is no extension

#

so there is a string(not none)

#

here i check if the length of string is 0

worn gorge
worn gorge
blazing crest
#

Anything else brother?

worn gorge
#

No but thanks for the help have a nice day

blazing crest
#

Haave a nice day

#

!close to close the thread

normal adderBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ help