File exists or not?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

File exists or not?

 
0
  #1
Aug 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 19
Reputation: OutOfReach is an unknown quantity at this point 
Solved Threads: 2
OutOfReach OutOfReach is offline Offline
Newbie Poster

Re: File exists or not?

 
0
  #2
Aug 22nd, 2008
First, import the os module:
  1. import os

Then check if the path exists:
  1. if os.path.exists(MY_PATH_HERE):
  2. #Do something...
  3. else:
  4. #Throw an error
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: File exists or not?

 
0
  #3
Aug 22nd, 2008
And os.path.isfile(name) will work for a file. Note that name is the full name, so include the full path.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: File exists or not?

 
0
  #4
Aug 25th, 2008
Thanks a lot.... Its working.

Cheers,
Dinil
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1820 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC