📜  Python|如何下载windows锁屏壁纸

📅  最后修改于: 2022-05-13 01:55:45.353000             🧑  作者: Mango

Python|如何下载windows锁屏壁纸

每当您打开 PC/笔记本电脑时,您是否曾在 Windows 10 锁定屏幕中看到这些酷炫的壁纸?
墙纸

每当我们连接到互联网时,它们都会随机变化。但有没有想过它背后的工作?好吧,这些图像存储在以下路径中:

C:\Users\[[Your Username]]\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets

但是这个故事有一个转折点。壁纸将看起来像这样。
截屏
这些实际上是没有扩展名的图像,即它们的扩展名已被删除。

您可能正在考虑一张一张地复制图像,然后一张一张地更改图像的扩展名,这也太手动了。
好吧,为了让你的生活更轻松, Python就在你身边。它将只为您完成任务,也只需一个代码。

下面是Python的实现——

注意:在桌面上创建一个名为 WALLPAPER 的文件夹。

import os
import shutil
  
os.chdir('C:\\')
username = os.environ['USERNAME']
  
# The folder which contains the wallpaper files
source = ("C:\\Users\\"+ username +"\\AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets\\")
  
# You will have to add the path of your
# destination here. Just make sure the
# folder exists on the desktop.
destination = ("C:\\Users\\"+ username +"\\Desktop\\WALLPAPER\\")
  
for the_file in os.listdir(destination):
      
    path_of_file = os.path.join(destination, the_file)
    base_file, ext = os.path.splitext(the_file)
  
    if ext ==".jpg":
        try:
            if os.path.isfile(path_of_file):
                os.unlink(path_of_file)
  
        except Exception as e:
            print(e)
              
for name_of_file in os.listdir(source):
    shutil.copy( source + name_of_file, destination)
    print(name_of_file)

但是,该文件夹仍然看起来像这样。
文件夹 1

那么接下来该怎么做呢?
请参阅下面的Python代码,将其作为副本保存在桌面上的同一 WALLPAPER 文件夹中并在那里运行。

下面是Python代码——

import os, sys
  
# It oversees all the file in the folder 
# and changes it with a proper extension.
for filename in os.listdir(os.path.dirname(os.path.abspath(__file__))):
    
  base_file, ext = os.path.splitext(filename)
    
  if ext == "":
    os.rename(filename, base_file + ".jpg")

执行后,文件夹将如下所示。将有一些图像不是壁纸,而是某些游戏或其他应用程序的图标。但是一旦你删除它们,你会得到一个很酷的文件夹,里面装满了很酷的壁纸。
最终的