473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python doesn't understand %userprofile%

In xp when I try os.path.getmtime("%userprofile/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 9064
On Jun 10, 8:56 am, bsag...@gmail.com wrote:
In xp when I try os.path.getmtime("%userprofile/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.getmtime("%userprofile%/dir/file")
Jun 27 '08 #2
bs*****@gmail.com wrote:
In xp when I try os.path.getmtime("%userprofile/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:

"%USERPROFILE%/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.getmtime (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.com wrote:
In xp when I try os.path.getmtime("%userprofile/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:

"%USERPROFILE%/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.getmtime (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.com wrote:
In xp when I try os.path.getmtime("%userprofile/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:

"%USERPROFILE%/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.getmtime (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.Desktop() 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.com wrote:
In xp when I try os.path.getmtime("%userprofile/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.expanduser("~/dir/file")
'C:\\Documents and Settings\\Duncan/dir/file'
Jun 27 '08 #6
On Jun 10, 2:09 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
bsag...@gmail.com wrote:
In xp when I try os.path.getmtime("%userprofile/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.expanduser("~/dir/file")

'C:\\Documents and Settings\\Duncan/dir/file'
"~" appears to look first at the HOME environment variable.
That is not necessarily the same as "USERPROFILE". On my machine it is
not.
Jun 27 '08 #7
dr******@gmail.com wrote:
On Jun 10, 2:09 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
>bsag...@gmail.com wrote:
>>In xp when I try os.path.getmtime("%userprofile/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.expanduser("~/dir/file")
'C:\\Documents and Settings\\Duncan/dir/file'

"~" appears to look first at the HOME environment variable.
That is not necessarily the same as "USERPROFILE". 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:
"%USERPROFILE%/dir/file".
os.environ('USERPROFILE') 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...@icqmail.comwrote:
On 00:11, mercoledì 11 giugno 2008 Tim Golden wrote:
"%USERPROFILE%/dir/file".

os.environ('USERPROFILE') 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
bs*****@gmail.com wrote:
>
In xp when I try os.path.getmtime("%userprofile/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.
For the record, the %PERCENT% syntax for looking up an environment variable
is just a feature of the XP command shell. It has no meaning to any other
part of Windows.

os.environ is the right answer.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #11
On Jun 11, 1:58*am, Tim Roberts <t...@probo.comwrote:
bsag...@gmail.com wrote:
In xp when I try os.path.getmtime("%userprofile/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.

For the record, the %PERCENT% syntax for looking up an environment variable
is just a feature of the XP command shell. *It has no meaning to any other
part of Windows.

os.environ is the right answer.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

You can use it at the Run command or in Explorer too. If I type
%username% in either place, it opens the following on my XP machine: C:
\Documents and Settings\Mike

Mike
Jun 27 '08 #12

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

Similar topics

699
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...
226
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...
49
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...
14
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"...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
137
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...
112
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...
17
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.