|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
The os.path module has a isfile function which'll return true if the file name you pass as an argument exits.
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
I've solved it!
it is... os.path.isfile(...filename/path...) Cheers for the help Solarbear ![]() Tryst |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
You learn something new everyday :-)
Cheers guys Tryst |
|
#7
|
|||
|
|||
|
Do not use from <module> import *. That is all.
|
|
#8
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Checking for an existing file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|