Python Forum

Full Version: Get file description
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Quote:I have the small code here, as it stands right now it works perfectly

What I'm trying to do is to get it to stop looking at program = r'\ccsetup551.exe'
I'm trying to get it to look in

with open(r'Program_Recovery2_Selection.txt', 'r') as file:
file4 = file.read().replace('\n', "")
file.close()

for the file name but every time I replace program with file4
it tells me it's unknown

except is only if the file does not have a description
it will display an unknown

As you can see I am using ccleaner.exe as an example
so how do I get this script to look in the Program_Recovery2_Selection.txt
and add it to the path

import win32api

file4 = ''


def get_file_description(windows_exe):
    global file4
    try:
        language, codepage = win32api.GetFileVersionInfo(windows_exe, '\\VarFileInfo\\Translation')[0]
        stringfileinfo = u'\\StringFileInfo\\%04X%04X\\%s' % (language, codepage, "FileDescription")
        description = win32api.GetFileVersionInfo(windows_exe, stringfileinfo)

        with open(r'Program_Recovery2_Selection.txt', 'r') as file:   <----- look in here
            file4 = file.read().replace('\n', "")
            file.close()

    except (Exception,):  # adding (Exception,) seem to work I don't know why
        description = "unknown"  # if the program doesn't have a description

    return description


path = r'\\MyCloudPR4100\Programs\Specialty tools'
program = r'\ccsetup551.exe'  # ccsetup551.exe     <--- not here
print(get_file_description(path + program))
This seems to work:

from pathlib import Path

# py -3.12 -m pip install pywin32
import pywintypes
from win32api import GetFileVersionInfo


def get_description(exe: str | Path) -> str:
    exe = str(exe)
    
    try:
        language, codepage = GetFileVersionInfo(exe, '\\VarFileInfo\\Translation')[0]
    except pywintypes.error:
        return "Unkown"
    
    return GetFileVersionInfo(exe, fr'\StringFileInfo\{language:04X}{codepage:04X}\FileDescription')



for exe in Path(r"C:\Windows\System32").glob("*.exe"):
    print(exe.name, get_description(exe), sep=": ")
(Nov-23-2023, 01:16 PM)DeaD_EyE Wrote: [ -> ]This seems to work:

from pathlib import Path

# py -3.12 -m pip install pywin32
import pywintypes
from win32api import GetFileVersionInfo


def get_description(exe: str | Path) -> str:
    exe = str(exe)
    
    try:
        language, codepage = GetFileVersionInfo(exe, '\\VarFileInfo\\Translation')[0]
    except pywintypes.error:
        return "Unkown"
    
    return GetFileVersionInfo(exe, fr'\StringFileInfo\{language:04X}{codepage:04X}\FileDescription')



for exe in Path(r"C:\Windows\System32").glob("*.exe"):
    print(exe.name, get_description(exe), sep=": ")
Quote:I actually try something similar to that
but I wanted to look in the text file

I actually just a little while ago figured out what the problem was
and I solve my problem
thank you everyone for helping out