Home > OS >  Checking to validate if user input exists as a file
Checking to validate if user input exists as a file

Time:04-12

I'm trying to create a script where users will enter a file path for processing. Currently, I'm trying to validate the users input (if the file exists, the processing will proceed, if not, the user will have to re enter the path to the file). My while loop currently looks like this:

from os import path

# Ask the user to insert file path for conversion.
user_input = input('Hello, please enter the path to the raw data trends file you wish to convert .\n'  
                   'Please include the full directory path and file extension (.csv).: ')

# Check if file path is correct
while path.isfile(user_input) == False:
    "I did not find the file at " str(user_input) '. Please check the directory path, spelling, and try again.: '
    user_input = input
else:
    print('Trends file found. Beginning conversion.')

Where I'm trying to validate it by checking to see if the user input path leads to a file, holding it in the while loop until path.isfile returns true. However, if I enter a term to deliberately throw an error, I get this:

TypeError: stat: path should be string, bytes, os.PathLike or integer, not method

What am I doing wrong that it can't handle an erroneous path correctly? Any help would be most appreciated!

CodePudding user response:

You need to print out a string and call input() as a function. In your code you are passing function name to user_input instead of function call. You also don't need an else statement. You can print that after the loop.

from os import path

# Ask the user to insert file path for conversion.
user_input = input('Hello, please enter the path to the raw data trends file you wish to convert .\n'  
                   'Please include the full directory path and file extension (.csv).: ')

# Check if file path is correct
while path.isfile(user_input) == False:
    print("I did not find the file at " str(user_input) '. Please check the directory path, spelling, and try again.: ')
    user_input = input()

CodePudding user response:

In your while loop you are not calling the input function. You might want to try something like:

# Check if file path is correct
while path.isfile(user_input) == False:
    user_input = input("I did not find the file at " str(user_input) '. Please check the directory path, spelling, and try again.: ')
else:
    print('Trends file found. Beginning conversion.')

CodePudding user response:

You have written user_input=input

here input is a function and is not a string instead You use user_input=input()

from os import path

# Ask the user to insert file path for conversion.
user_input = input('Hello, please enter the path to the raw data trends file you wish to convert .\n'  
                   'Please include the full directory path and file extension (.csv).: ')

# Check if file path is correct
while path.isfile(user_input) == False:
    "I did not find the file at " str(user_input) '. Please check the directory path, spelling, and try again.: '
    user_input = input()
else:
    print('Trends file found. Beginning conversion.')
  • Related