如何获得桌面位置? [英] How to get Desktop location?

查看:81
本文介绍了如何获得桌面位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows上使用Python,我希望脚本的一部分将文件从某个目录(我知道它的路径)复制到桌面上。

I'm using Python on Windows and I want a part of my script to copy a file from a certain directory (I know its path) to the Desktop.

我用了这个

shutil.copy(txtName, '%HOMEPATH%/desktop')

txtName 是txt文件的名称(具有完整路径)。

While txtName is the txt File's name (with full path).

我收到错误:

IOError: [Errno 2] No such file or directory: '%HOMEPATH%/DESKTOP'

有什么帮助吗?

我希望脚本可在任何计算机上运行。

I want the script to work on any computer.

推荐答案

您可以使用 os.environ [ HOMEPATH] 来获取路径。现在,它实际上是在尝试查找%HOMEPATH%/ Desktop 而不替换实际路径。

You can use os.environ["HOMEPATH"] to get the path. Right now it's literally trying to find %HOMEPATH%/Desktop without substituting the actual path.

也许是这样的:

shutil.copy(txtName, os.path.join(os.environ["HOMEPATH"], "Desktop"))

这篇关于如何获得桌面位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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