将文件复制到 Windows 桌面

Copy file to Windows Desktop

我想将文件从一个位置复制到当前用户的桌面路径。要在 Windows CMD 中获取它,它将是 %userprofile%\Desktop

我试过了:

source_path = r"\cpd-sts\UsersSoftware\Cognos"
dest_path = os.path.join(os.environ["HOMEPATH"], "Desktop")
file_name = "\Impromptu_User.zip"
shutil.copyfile(source_path + file_name, dest_path + file_name)

输出错误为:

FileNotFoundError: [Errno 2] No such file or directory: '\Users\joshua\Desktop\Impromptu_User.zip'

Unix 使用 / 进行文件分隔,而不是 \。请尝试以下操作:

source_path = r"/cpd-sts/UsersSoftware/Cognos"
dest_path = os.path.join(os.environ["HOMEPATH"], "Desktop")
file_name = "/Impromptu_User.zip"
shutil.copyfile(source_path + file_name, dest_path + file_name)

我找到了解决方案:

source_path = r"\cpd-sts\UsersSoftware\Cognos"
dest_path = ("C:" + (os.path.join(os.environ["HOMEPATH"], "Desktop")) + "/Cognos")
file_name = "\Impromptu_User.zip"
shutil.copyfile(source_path + file_name, dest_path + file_name)