473,320 Members | 2,094 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,320 software developers and data experts.

Thanks for help re: %userprofile%

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
1 1567
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: KhmerBoyz | last post by:
Hi, I am not sure whether this is the right news group to ask but I'm trying anyway. This is my first time development pocket pc in vb.net, i used to do this in eVB. As part of the requirement...
0
by: Kurt Watson | last post by:
I’m having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web...
1
by: Locke Nash Cole | last post by:
I'm making a utility similar to MSConfig, I've found it easy enough to obtain the startup items from the registry for the current user and local machine... but what about the Startup folder on the...
0
by: Burak Gunay | last post by:
Hello, I have a web solution, which has a vb class project in it, that serves as the BLL. This code, which creates a new user profile and copies data from the old profile into it, works just...
11
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package...
30
by: carlos123 | last post by:
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on. 1. Reading a file. 2. Taking that data read from the file and putting it into...
8
by: Perl Beginner | last post by:
Hello All, I have Perl code that looks like the following. $file is pointing to a directory on the C: drive that has 2 text files in it (let's say text_file_name1.txt and text_file_name2.txt).The...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
11
by: bsagert | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.