如何获取桌面位置?

2023-08-31Python开发问题
5

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

问题描述

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

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

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11