473,577 Members | 3,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python doesn't understand %userprofile%

In xp when I try os.path.getmtim e("%userprofi le/dir/file%") Python
bites back with "cannot find the path specified" Since my script has
to run on machines where the username is unspecified I need a fix.
Thanks in advance.
Jun 27 '08 #1
11 9129
On Jun 10, 8:56 am, bsag...@gmail.c om wrote:
In xp when I try os.path.getmtim e("%userprofi le/dir/file%") Python
bites back with "cannot find the path specified" Since my script has
to run on machines where the username is unspecified I need a fix.
Thanks in advance.
oops that should be os.path.getmtim e("%userprofile %/dir/file")
Jun 27 '08 #2
bs*****@gmail.c om wrote:
In xp when I try os.path.getmtim e("%userprofi le/dir/file%") Python
bites back with "cannot find the path specified" Since my script has
to run on machines where the username is unspecified I need a fix.
Well I can see a few problems here.

First is that putting percent signs around the whole path is
never going to work anyway. You want something like:

"%USERPROFI LE%/dir/file".

Secondly, the expansion of environment variables like
USERPROFILE is done for you by the shell or the command
prompt. You have to do it for yourself if you're opening your
own files. You want something like:

import os
print os.path.getmtim e (os.path.join (os.environ['USERPROFILE'], "ntuser.ini "))

But finally, what do you mean "run on machines where the username is
unspecified"? If you mean: where no user is logged in, then you won't
have a (meaningful) userprofile in any case: it might be the Default User
profile; I'm not sure. But is that what you want?

You *can* use the functions in the win32profile module of the pywin32
packages to find out various things about profiles directories, but things
can get quite complicated if users have roaming profiles and the like.

TJG
Jun 27 '08 #3
Lie
On Jun 10, 11:11*pm, Tim Golden <m...@timgolden .me.ukwrote:
bsag...@gmail.c om wrote:
In xp when I try os.path.getmtim e("%userprofi le/dir/file%") Python
bites back with "cannot find the path specified" Since my script has
to run on machines where the username is unspecified I need a fix.

Well I can see a few problems here.

First is that putting percent signs around the whole path is
never going to work anyway. You want something like:

"%USERPROFI LE%/dir/file".

Secondly, the expansion of environment variables like
USERPROFILE is done for you by the shell or the command
prompt. You have to do it for yourself if you're opening your
own files. You want something like:

import os
print os.path.getmtim e (os.path.join (os.environ['USERPROFILE'], "ntuser.ini "))

But finally, what do you mean "run on machines where the username is
unspecified"? If you mean: where no user is logged in, then you won't
have a (meaningful) userprofile in any case: it might be the Default User
profile; I'm not sure. But is that what you want?
I think what he meant is where he doesn't know in advance the name of
the user name.
You *can* use the functions in the win32profile module of the pywin32
packages to find out various things about profiles directories, but things
can get quite complicated if users have roaming profiles and the like.

TJG
Jun 27 '08 #4
On Jun 10, 11:11*am, Tim Golden <m...@timgolden .me.ukwrote:
bsag...@gmail.c om wrote:
In xp when I try os.path.getmtim e("%userprofi le/dir/file%") Python
bites back with "cannot find the path specified" Since my script has
to run on machines where the username is unspecified I need a fix.

Well I can see a few problems here.

First is that putting percent signs around the whole path is
never going to work anyway. You want something like:

"%USERPROFI LE%/dir/file".

Secondly, the expansion of environment variables like
USERPROFILE is done for you by the shell or the command
prompt. You have to do it for yourself if you're opening your
own files. You want something like:

import os
print os.path.getmtim e (os.path.join (os.environ['USERPROFILE'], "ntuser.ini "))

But finally, what do you mean "run on machines where the username is
unspecified"? If you mean: where no user is logged in, then you won't
have a (meaningful) userprofile in any case: it might be the Default User
profile; I'm not sure. But is that what you want?

You *can* use the functions in the win32profile module of the pywin32
packages to find out various things about profiles directories, but things
can get quite complicated if users have roaming profiles and the like.

TJG
Tim,

I'm surprised you didn't mention your excellent winshell utility. I
use it for this sort of issue all the time where I need to update
files on login and I don't know the user's name beforehand.

The winshell.Deskto p() one has been a life saver and I think the OP
could probably use winshell for their problem. Or I may be completely
off my rocker.

Either way, here's the link: http://timgolden.me.uk/python/winshell.html

Mike
Jun 27 '08 #5
bs*****@gmail.c om wrote:
In xp when I try os.path.getmtim e("%userprofi le/dir/file%") Python
bites back with "cannot find the path specified" Since my script has
to run on machines where the username is unspecified I need a fix.
Thanks in advance.
>>os.path.expan duser("~/dir/file")
'C:\\Documents and Settings\\Dunca n/dir/file'
Jun 27 '08 #6
On Jun 10, 2:09 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
bsag...@gmail.c om wrote:
In xp when I try os.path.getmtim e("%userprofi le/dir/file%") Python
bites back with "cannot find the path specified" Since my script has
to run on machines where the username is unspecified I need a fix.
Thanks in advance.
>os.path.expand user("~/dir/file")

'C:\\Documents and Settings\\Dunca n/dir/file'
"~" appears to look first at the HOME environment variable.
That is not necessarily the same as "USERPROFIL E". On my machine it is
not.
Jun 27 '08 #7
dr******@gmail. com wrote:
On Jun 10, 2:09 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
>bsag...@gmail. com wrote:
>>In xp when I try os.path.getmtim e("%userprofi le/dir/file%") Python
bites back with "cannot find the path specified" Since my script has
to run on machines where the username is unspecified I need a fix.
Thanks in advance.
os.path.exp anduser("~/dir/file")
'C:\\Documen ts and Settings\\Dunca n/dir/file'

"~" appears to look first at the HOME environment variable.
That is not necessarily the same as "USERPROFIL E". On my machine it is
not.
There was quite a debate over that on python-dev earlier
this year. In short, it's not easy to identify exactly what
"~" means on a Windows box. The implementer of that patch
took the view that HOME comes first and then USERPROFILE,
finally HOMEDRIVE/SHARE/PATH. On my machine at work,
the latter should take precedence as they are set via
my domain logon. Other people's mileage may vary. In addition,
the code assumes that the home for any *other* user can
always be derived from the *current* user's home.
Which will not always be the case.

All that is essentially why the user-specific functions
exposed in win32profile rely on a logon token to operate.

(Bit of a rant, but just to highlight that things are
seldom what they seem).

TJG
Jun 27 '08 #8
On 00:11, mercoledì 11 giugno 2008 Tim Golden wrote:
"%USERPROFI LE%/dir/file".
os.environ('USE RPROFILE') should return an info regarding that environment
variable.
I guess that, not yet tried.
--
Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html
Jun 27 '08 #9
On Jun 10, 6:51*pm, TheSaint <fc14301...@icq mail.comwrote:
On 00:11, mercoledì 11 giugno 2008 Tim Golden wrote:
"%USERPROFI LE%/dir/file".

os.environ('USE RPROFILE') should return an info regarding that environment
variable.
I guess that, not yet tried.
--
Mailsweeper Home :http://it.geocities.com/call_me_not_now/index.html
I found:

from os import environ
r'%(HOMEPATH)s\ My Documents\My Pictures\pycon. bmp'% environ

But 'os.join' is supposedly more correct.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
33646
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
226
12412
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
49
2828
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville Vainio http://www.students.tut.fi/~vainio24
14
2840
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon" in the subject of any post to this newsgroup. Please ignore the old thread. I've also updated my webpage http://ece.arizona.edu/~edatools/Python...
68
5817
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
137
7012
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very pragmatic - 3) I usually move forward when I get the gut feeling I am correct - 4) Most likely because of 1), I usually do not manage to fully explain...
112
13789
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them because they smell "Javaish." But now my code base is expanding and I'm beginning to appreciate the wisdom behind them. I welcome example code and...
17
8082
by: DanielJohnson | last post by:
how to use the combination function in python ? For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 Please help, I couldnt find the function through help.
30
2695
by: Ivan Reborin | last post by:
Hello everyone, I was wondering if anyone here has a moment of time to help me with 2 things that have been bugging me. 1. Multi dimensional arrays - how do you load them in python For example, if I had: ------- 1 2 3 4 5 6
0
7845
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8121
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8286
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8143
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6517
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5340
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3779
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3797
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1385
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.