Thanks for help re: %userprofile%

bsagert at gmail.com bsagert at gmail.com
Wed Jun 11 00:33:50 EDT 2008


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.lang.python/browse_thread/thread/59cc71e92bef1ee2?hl=en#



More information about the Python-list mailing list