Re: [Tutor] [Linux] open a file in any home ~ ?

2006-04-24 Thread kakada
learner404 wrote:
 It works great, thanks very much to the three of you for these
 light-speed answers ... I love this list !

 Wesley, I've just pre-order your new edition Core Python programming
 on amazon France, it looks great. :)

 Thanks  
I love this list too.

Da
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Linux] open a file in any home ~ ?

2006-04-19 Thread Matthew White
os.getenv('HOME') will return the user's home directory as long as that
environment variable is set.

you can also use the pwd module:

 pwd.getpwnam('mtw')
('mtw', 'x', 1000, 1000, ',,,', '/home/mtw', '/bin/bash')

-mtw

On Wed, Apr 19, 2006 at 07:55:14PM +0200, learner404 ([EMAIL PROTECTED]) wrote:
 Hello,
 
 I want to read a configuration file from a small python app (user
 preferences).
 
 The .myapp.conf is in the home folder of the user.
 
 if I do:
 
 f=open(/home/user1/.myapp.conf,r)  #it works
 
 obviously I won't know the home user folder name then I wanted to use:
 
 f=open(~/.myapp.conf,r)  # but it returns a IOError: [Errno 2] No such
 file or directory:
 
 How can I do to access this file whatever the user is ?
 
 Thanks for your help.
 
 ---

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


-- 
Matthew White - District Systems Administrator
Tigard/Tualatin School District
503.431.4128

The greatest thing in this world is not so much where we are, but in
what direction we are moving.   -Oliver Wendell Holmes

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Linux] open a file in any home ~ ?

2006-04-19 Thread w chun
  f=open(~/.myapp.conf,r)  # but it returns a IOError: [Errno 2] No such
 file or directory:

  How can I do to access this file whatever the user is ?

use os.path.expanduser(path)

http://www.python.org/doc/2.4.3/lib/module-os.path.html

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Linux] open a file in any home ~ ?

2006-04-19 Thread learner404
It works great, thanks very much to the three of you for these light-speed answers ... I love this list ! 
Wesley, I've just pre-order your new edition Core Python programming on amazon France, it looks great. :)

Thanks 

On 19/04/06, w chun [EMAIL PROTECTED] wrote:
f=open(~/.myapp.conf,r)# but it returns a IOError: [Errno 2] No such file or directory:How can I do to access this file whatever the user is ?use os.path.expanduser
(path)http://www.python.org/doc/2.4.3/lib/module-os.path.htmlhope this helps!-- wesley- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001http://corepython.comwesley.j.chun :: wescpy-at-gmail.com
python training and technical consultingcyberweb.consulting : silicon valley, cahttp://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Linux] open a file in any home ~ ?

2006-04-19 Thread Paidhi Aiji
Hi,

You can use expanduser() from os.path for this:

import os.path
homedir = os.path.expanduser('~user1')
file_to_open = os.path.join(homedir, '.myapp.conf')
f = open(file_to_open, 'r')


Regards,
  -Markus-


Quoting learner404 [EMAIL PROTECTED]:

 Hello,

 I want to read a configuration file from a small python app (user
 preferences).

 The .myapp.conf is in the home folder of the user.

 if I do:

 f=open(/home/user1/.myapp.conf,r)  #it works

 obviously I won't know the home user folder name then I wanted to use:

 f=open(~/.myapp.conf,r)  # but it returns a IOError: [Errno 2] No such
 file or directory:

 How can I do to access this file whatever the user is ?

 Thanks for your help.

 ---





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Linux] open a file in any home ~ ?

2006-04-19 Thread Alan Gauld
 The .myapp.conf is in the home folder of the user.
 ...
 obviously I won't know the home user folder name then I wanted to use:
 How can I do to access this file whatever the user is ?

You can get the user name using getpass.getuser() as described in 
the OS topic of my tutor under the Securiity heading.

You can also try using os.getenv() or the os.environ variable 
to look up the HOME environment variable, also discussed 
in the OS topic.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Linux] open a file in any home ~ ?

2006-04-19 Thread w chun
we are all happy to help.  it is really good that you were able to get
it working so fast!  also, if you want to do any kind of pattern
matching with * or ?, then check out the glob module.

merci!  le livre does not look that good from here... it is a mess
and i have to clean it up before giving it to the publisher!  :-)

cheers,
-wesley


On 4/19/06, learner404 [EMAIL PROTECTED] wrote:
 It works great, thanks very much to the three of you for these light-speed
 answers ... I love this list !

  Wesley, I've just pre-order your new edition Core Python programming on
 amazon France, it looks great. :)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Linux] open a file in any home ~ ?

2006-04-19 Thread Alan Gauld
  How can I do to access this file whatever the user is ?

 use os.path.expanduser(path)

Neat Wesley, I've  never noticed that one before.

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor