By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Thanks for help re: %userprofile%

P: n/a
The python community is very helpful to newbies like me. I did however
manage to solve my problem in the meantime. I needed the modification
time of certain files on various computers, but I didn't know the
usernames ahead of time, so I used windows %userprofile% method.
Python likes forward slashes in file names, whereas windows likes back
slashes. Here is my script.

import os, re
u = os.getenv("USERPROFILE")
# python returns "c:\\documents and Settings\\user"
# note the escaped backslashes which windows hates.
# let's repair that with re.sub
u = re.sub( r"\\", "/", u)
f = u+"/dir1/file1"
mod = os.path.getmtime(f)
# success, now do something

c = "copy '%userprofile%\dir1\file1' c:\dir2\file2"
# note back slashes here which windows tolerates.
# In the os.system context, python delivers unescaped slashes.
os.system(c)
# success

I'm a retired old fart trying to learn python so I welcome criticism
and advice. My original post was at
http://groups.google.ca/group/comp.l...bef1ee2?hl=en#
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
bs*****@gmail.com wrote:
>The python community is very helpful to newbies like me. I did however
manage to solve my problem in the meantime. I needed the modification
time of certain files on various computers, but I didn't know the
usernames ahead of time, so I used windows %userprofile% method.
Python likes forward slashes in file names, whereas windows likes back
slashes.
Your last sentence is not true at all. The Windows APIs accept forward
slashes anywhere they accept backward slashes. It is only the command
shell insists on back slashes.
>Here is my script.

import os, re
u = os.getenv("USERPROFILE")
# python returns "c:\\documents and Settings\\user"
# note the escaped backslashes which windows hates.
What??? This is not only false, it is completely backwards from what you
just said. The string does NOT actually contain any doubled backslashes.
You only SEE that because of the way you are displaying them to your
terminal. This string:

x = "\\\n"

contains exactly two characters: a backslash, and a newline. Try it and
see.
># let's repair that with re.sub
u = re.sub( r"\\", "/", u)
That doesn't do what you think it does. r"\\" contains two characters,
both backslashes. However, your source string doesn't CONTAIN any doubled
backslashes. So, this statement does nothing.

Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>x = "\\\n"
len(x)
2
>>x = "\\" # contains one character: a backslash
len(x)
1
>>x
'\\'
>>x = r"\\" # contains two characters: both backslashes
len(x)
2
>>x # but when Python displays it, it escapes them
'\\\\'
>>>
>f = u+"/dir1/file1"
mod = os.path.getmtime(f)
# success, now do something
It would also have succeeded with all backslashes, or with mixed forward
and backslashes. You're focussing on the wrong things here.
>c = "copy '%userprofile%\dir1\file1' c:\dir2\file2"
# note back slashes here which windows tolerates.
That is WRONG, and it only worked by accident. If your directory names had
been "index" or "name", you would have found this out. The correct way to
write that is either:
c = "copy '%USERPROFILE%\\dir1\\file1' c:\\dir2\\file2"
or
c = r"copy '%USERPROFILE%\dir1\file1' c:\dir2\file2"

># In the os.system context, python delivers unescaped slashes.
os.system(c)
# success
There is no reason to go to an external program for this at all. Just do
this:
import shutil
shutil.copyfile( sourcefilename, destfilename )
>I'm a retired old fart trying to learn python so I welcome criticism
and advice.
Tell us what you are really trying to do, and we can offer some simple
scripts that you can use as an example.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.