954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

File exists or not?

Hi,

I am using a dialog box where user selects a .txt file from the local drive. The selected file along with the folder path is displayed in a textbox on the form. The user can now edit the textbox. On click on 'Proceed' button, I am saving that path.

Now if the user edits the file name(gives a file name which does not exist) the application should throw an error. How to check wheteher that file name exists in the local drive or not???.
If it does not exists then a message box with error message should be shown to the user.

Please help!....

Regards,
Dinil

dinilkarun
Posting Whiz in Training
206 posts since Feb 2008
Reputation Points: 18
Solved Threads: 0
 

First, import the os module:

import os


Then check if the path exists:

if os.path.exists(MY_PATH_HERE):
    #Do something...
else:
    #Throw an error
OutOfReach
Newbie Poster
19 posts since Aug 2008
Reputation Points: 10
Solved Threads: 2
 

And os.path.isfile(name) will work for a file. Note that name is the full name, so include the full path.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Thanks a lot.... Its working.

Cheers,
Dinil

dinilkarun
Posting Whiz in Training
206 posts since Feb 2008
Reputation Points: 18
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You