PDA

View Full Version : Best method to determine the presence of a file


kozumo
05-29-2002, 04:18 AM
I'm learning Python now (I'm still not used to not using semi-colons) and I was wondering what's the best way to detect the presence of a file in a directory. Also I want to be able to refer to the user's homedirectory. I tried replace '/home/kozumo/' with ~, but it didn't work. I'm guessing it's Shell specific?

Here's a snippet of my code:

import dircache

ls = dircache.listdir('/home/kozumo/')

present,i = 0,0
for i in range(len(ls)):
if ".qtask" == ls[i]:
present = 1

Not very elegant. Is there a better way?

Thanks

Strike
05-29-2002, 04:45 AM
In Python 2.2 and later:

try:
myFile = file("somefile", "r")
except IOError:
print "Couldn't open somefile"

earlier than 2.2:

try:
myFile = open("somefile", "r")
except IOError:
print "Couldn't open somefile"

Benny
05-29-2002, 08:46 AM
You could also do something like this:


import os

if os.path.exists("path to somefile") == 0:
print "File doesn't exist!"
else:
print "File exists"


The exists() function returns 1 if the file exists and zero if it doesn't.

GnuVince
05-29-2002, 08:58 AM
I would go with try/except

inkedmn
05-29-2002, 12:14 PM
i actually like both methods:

try/except on the file = open(filename)

-or-

if os.path.exists(path-to-file) = 1

it depends on what you want to do with the file, if you're copying it or something NOT directly involved with it's contents, i'd use the second method. if you're going to be parsing the contents of the file or in some other way manipulating it, use the first way.

just my $.02

Strike
05-29-2002, 12:23 PM
Yeah, I'll second what inkedmn said. I just chose the one that used the builtin method for "lack of namespace pollution" sake ;)

inkedmn
05-29-2002, 12:43 PM
oh, and for the home dir thing, you could just do something like this:

home = '/home/username'

and then refer to it as home throughout the script (instead of always typing out the path

Strike
05-29-2002, 12:50 PM
Actually, the best way to do that is to use the os.path.join function since it's more platform-independent.

home_path = os.path.join("home", "username")

kozumo
05-29-2002, 01:08 PM
Thanks a lot guys, I'm going to use the try and except method, because I will be modifying the contents of the file.

Actually, I was hoping I could retrieve the username or better yet the home directory (some systems do not have their user's home directory under /home/username) from the environment rather than typing it out, that's why I thought about using ~

What I want to do is create a file in the user's home directory.

Strike
05-29-2002, 01:37 PM
Well, if you want the home directory of the user that the script is running as, then that is in

os.environ['HOME']

kozumo
05-29-2002, 01:47 PM
You've been overly helpful, Strike. Thanks

Strike
05-29-2002, 04:11 PM
Originally posted by kozumo
You've been overly helpful, Strike. Thanks
Bah, no such thing as overly helpful :)

jemfinch
05-30-2002, 06:12 PM
Originally posted by kozumo
Actually, I was hoping I could retrieve the username or better yet the home directory (some systems do not have their user's home directory under /home/username) from the environment rather than typing it out, that's why I thought about using ~

Use os.environ.

Jeremy

Bradmont
05-30-2002, 09:33 PM
Originally posted by jemfinch


Use os.environ.

Jeremy

jemfinch! I thought you were leaving forever! How's it going?

GnuVince
05-30-2002, 09:36 PM
Brad: You should hang in the OCaml forum ;)

Strike
05-31-2002, 08:41 AM
Originally posted by jemfinch


Use os.environ.

Jeremy
Why didn't I think of that?

/me looks up

Oh, I did. ;)