SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old February 10th, 2004, 11:09 AM
Tryst Tryst is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 146 Tryst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 26 m 49 sec
Reputation Power: 5
Checking for an existing file

Hi all, I am new to python and an carrying out a bit of work in which I must retrieve emails and copy its Body of text into a text file. But before I can write this text to a file I need to check whether the file I am writing to already exists or not. Is there a specific function I can use to do this?

I've done some searching online and can't really find anything useful, apart from a mention of the Class 'path'???

Cheers peps

Tryst
__________________
Tryst

Reply With Quote
  #2  
Old February 10th, 2004, 12:11 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
The os.path module has a isfile function which'll return true if the file name you pass as an argument exits.

Reply With Quote
  #3  
Old February 10th, 2004, 12:40 PM
Tryst Tryst is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 146 Tryst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 26 m 49 sec
Reputation Power: 5
Hi, I have tested some code with the information you have given me, and am coming up with an error (error states that the name/function isfile does not exist).

This is the simple piece of code...

import os.path

filename = "test HTML.txt"
if(isfile(filename)):
print "It exists!!"
else:
print "It does not exist!"

Why isn't it recognising the function isfile?

Cheers

Reply With Quote
  #4  
Old February 10th, 2004, 12:48 PM
Tryst Tryst is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 146 Tryst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 26 m 49 sec
Reputation Power: 5
I've solved it!

it is...

os.path.isfile(...filename/path...)

Cheers for the help Solarbear

Tryst

Reply With Quote
  #5  
Old February 10th, 2004, 04:04 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
You could have also imported the functions using the "form module import function" form and you're first example would have worked fine ...

Code:
from os.path import *

path = "test HTML.txt"
if(isfile(path)):
    print "It exists!!"
else:
    print "It does not exist!"


Also you dont have to import os.path directly, although its perfectly a valid way to get os.path, i just find it much nicer to import os which makes os.path available .

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #6  
Old February 10th, 2004, 04:16 PM
Tryst Tryst is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 146 Tryst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 26 m 49 sec
Reputation Power: 5
You learn something new everyday :-)

Cheers guys

Tryst

Reply With Quote
  #7  
Old February 10th, 2004, 05:59 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Do not use from <module> import *. That is all.

Reply With Quote
  #8  
Old February 10th, 2004, 07:03 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Comon . There can be nothing that wrong with using import * as long as you know what you're getting. Although i myself like to see where i'm calling functions from. Unless you have somthing i havnt considered to add?

Mark

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Checking for an existing file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway