473,573 Members | 5,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("USER PROFILE")
# python returns "c:\\docume nts 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.getmtim e(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 1580
bs*****@gmail.c om 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("USER PROFILE")
# python returns "c:\\docume nts 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.getmtim e(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
1352
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 it is requires to development in ..net, however i couldn't get any where much because i need the answers for the following questions: 1. How do i...
0
1689
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 browser that Hotmail does not support. If you continue to use your current browser software we cannot guarantee that Hotmail will work correctly for...
1
1779
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 Start Menu? I've found there are 2 system variables that could help me but.. what is the proper way to get these locations? And be...
0
953
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 fine in an aspx.vb code behind file. Dim oldRequestProfile As ProfileCommon = Profile.GetProfile(txtId.Text)
11
26552
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 and PEAR is in c:\wamp\php\pear I modified php.ini in the c:\wamp\php directory to reflect the actual path, but even stopping and restarting my...
30
2270
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 an array. 3. A way to access the array, and check if it matches what the user inputed (like a login program) This is what the text file looks...
8
6937
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 lines of code to print out the names of the text files in the directory are: foreach $file(@result_files) { print "$file\n"; } Can...
5
2146
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 quiz i have to do a password and a login. I managed to do the password but when i tried to join this to the whole program its not working. This is the...
11
9127
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 fix. Thanks in advance.
0
7709
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8039
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
8218
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...
1
7800
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8091
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
6435
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
3738
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
3744
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1330
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.