-1

so there are alot of files with .lnk extension in the start menu folder C:\ProgramData\Microsoft\Windows\Start Menu\Programs i want to print all those file names so i tried this code:

import os
import glob
startmenu = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs'

os.chdir(startmenu)
for file in glob.glob("**/*.lnk", recursive = True):
    print(file)

it prints the link to the files, but i want to print only the file names with the extension of ".lnk"

Praveen
  • 106
  • 1
  • 11
  • Take a look at [this question](https://stackoverflow.com/questions/8384737/extract-file-name-from-path-no-matter-what-the-os-path-format) regarding how to extract the filename from path strings – mapto Oct 08 '20 at 10:13

1 Answers1

0

Convert the absolute path to list then take the last element from the list. See the below code.

import os
import glob

startmenu = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs'

os.chdir(startmenu)
for file in glob.glob("**/*.lnk", recursive=True):
    print(os.path.split(file)[-1])