Python不理解%userprofile% [英] Python doesn't understand %userprofile%

查看:316
本文介绍了Python不理解%userprofile%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xp中,当我尝试os.path.getmtime("%userprofile / dir / file%&); Python

咬回来时找不到指定的路径由于我的脚本在未指定用户名的机器上运行,因此需要修复。

提前致谢。

解决方案

6月10日上午8:56,bsag ... @ gmail.com写道:


在xp中我尝试os .path.getmtime("%userprofile / dir / file%&); Python

咬回无法找到指定的路径由于我的脚本有

在未指定用户名的机器上运行,我需要修复。

提前谢谢。



oops应该是os.path.getmtime("%userprofile%/ dir / file")


< blockquote> bs*****@gmail.com 写道:


在xp中我尝试os.path.getmtime("%userprofile / dir / file%&); Python

咬回无法找到指定的路径由于我的脚本有

在未指定用户名的机器上运行,我需要修复。



我在这里可以看到一些问题。


首先,在整个路径上放置百分号是永远不会工作。你需要这样的东西:


"%USERPROFILE%/ dir / file"。


其次,扩展环境变量如

USERPROFILE由shell或命令

提示符完成。如果您打开自己的文件,则必须自己动手。你需要这样的东西:


import os

print os.path.getmtime(os.path.join(os.environ [''USERPROFILE''] ,ntuser.ini))


但最后,你的意思是在用户名为

未指定的机器上运行?如果你的意思是:没有用户登录,那么你不会在任何情况下都有一个(有意义的)用户配置文件:它可能是默认用户

配置文件;我不确定。但这就是你想要的吗?


你*可以*使用pywin32

包的win32profile模块中的函数来查找有关配置文件目录的各种信息但是如果用户有漫游配置文件之类的东西,那么

会变得非常复杂。


TJG


< blockquote> 6月10日,11:11 * pm,Tim Golden< m ... @ timgolden.me.ukwrote:


bsag ... @ gmail。 com写道:


在xp中我尝试os.path.getmtime("%userprofile / dir / file%&); Python

咬回找不到指定的路径由于我的脚本有

在未指定用户名的机器上运行,我需要修复。



我在这里可以看到一些问题。


首先,在整个路径上放置百分号是永远不会工作。你需要这样的东西:


"%USERPROFILE%/ dir / file"。


其次,扩展环境变量如

USERPROFILE由shell或命令

提示符完成。如果您打开自己的文件,则必须自己动手。你需要这样的东西:


import os

print os.path.getmtime(os.path.join(os.environ [''USERPROFILE''] ,ntuser.ini))


但最后,你的意思是在用户名为

未指定的机器上运行?如果你的意思是:没有用户登录,那么你不会在任何情况下都有一个(有意义的)用户配置文件:它可能是默认用户

配置文件;我不确定。但这就是你想要的吗?



我认为他的意思是他事先并不知道用户名

的名字。


您*可以*使用pywin32

软件包的win32profile模块中的函数来查找有关配置文件目录的各种信息,但事情
如果用户有漫游配置文件等,
会变得非常复杂。


TJG


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.

解决方案

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")


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


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


这篇关于Python不理解%userprofile%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆