943,643 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2987
  • Python RSS
Aug 22nd, 2008
0

File exists or not?

Expand Post »
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
Similar Threads
Reputation Points: 18
Solved Threads: 0
Posting Whiz in Training
dinilkarun is offline Offline
206 posts
since Feb 2008
Aug 22nd, 2008
0

Re: File exists or not?

First, import the os module:
Python Syntax (Toggle Plain Text)
  1. import os

Then check if the path exists:
Python Syntax (Toggle Plain Text)
  1. if os.path.exists(MY_PATH_HERE):
  2. #Do something...
  3. else:
  4. #Throw an error
Reputation Points: 10
Solved Threads: 2
Newbie Poster
OutOfReach is offline Offline
19 posts
since Aug 2008
Aug 22nd, 2008
0

Re: File exists or not?

And os.path.isfile(name) will work for a file. Note that name is the full name, so include the full path.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Aug 25th, 2008
0

Re: File exists or not?

Thanks a lot.... Its working.

Cheers,
Dinil
Reputation Points: 18
Solved Threads: 0
Posting Whiz in Training
dinilkarun is offline Offline
206 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: what is strong point of python in comparison of othere language
Next Thread in Python Forum Timeline: Py2exe help.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC