0

Hello guys,
I have a program created that opens a program and execute it as well.

For example:

filename = raw_input('Input a valid NAPS filename -> ')
execfile(filename)

I put my file helloworld.naps (my file extension)
And it execute smoothly.

Now my question is, how to verify the user input if there is a .naps file extension in their file.
If they have not, then there will be an error that displays 'You have entered an invalid filename'. I got the logic but I dunno how to code it.

2
Contributors
2
Replies
5
Views
4 Years
Discussion Span
Last Post by nerdnapster
2

Well, you have

>>> import os
>>> filename = "foo/bar/baz/qux.naps"
>>> name = os.path.basename(filename)
>>> name
'qux.naps'
>>> base, extension = os.path.splitext(name)
>>> base
'qux'
>>> extension
'.naps'

and also

>>> os.path.exists(filename) # check if file exists
False
# or
>>> os.path.isfile(filename) # check if a regular file with that name exists
False

Edited by Gribouillis

2

I tried this code of yours and put some if-elif statements and its now fully working.
I'm really thankful for the quick response sir. Soon if I quite learn more about python, I'll contribute to this community. ^__^

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.