Python | How to download wallpaper for Windows lock screen

Python Methods and Functions

Whenever we are connected to the Internet, they will change randomly. But have you ever wondered what is behind this? Well, these images are stored in the following path:

 C: Users [[Your Username]] AppDataLocalPackagesMicrosoft.Windows.ContentDeliveryManager_cw5n1h2txyewyLocalStateAssets 

But there is a twist in the story. The wallpaper will look like this. 

These are actually images without their extensions, that is, their extension is removed.

You can think of copying images one by one and then changing the image extension one by one, and this is also manual. 
Well, to make your life easier, Python is for you. This will do the task just for you, this also with one code.

Below is the Python implementation —

Note. Create a folder named WALLPAPER on your desktop.

import os

import shutil

 

os.chdir ( `C: ` )

username = os.environ [ `USERNAME` ]

  
# Folder with wallpaper files

source = ( "C: Users " + username + " AppData Local Packages Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy  LocalState Assets ")

  
# You will need 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.l istdir (source):

shutil.copy (source + name_of_file, destination)

print (name_of_file)

But the folder will still look like this. 

So what to do next?
Take a look Python code below, save it as a copy in the same WALLPAPER folder on your desktop and run it there.

Below is the Python code —

import os, sys

 
# It monitors all files in the folder
# and changes it with the correct extension.

for filename in os.listdir (os .path.dirname (os.path.abspath (__ file__))):

 

base_file, ext = os.path.split (ext (filename)

  

if ext = = "":

  os.rename (filename, base_file + ". jpg " )

After execution, the folder will look like this. There will be some images that will not be wallpapers but will instead be an icon for some games or other applications. But once you delete them, you have a cool folder full of cool wallpapers. 





Get Solution for free from DataCamp guru