Python Forum

Full Version: return none
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone I have this small script here
which I'm sure a lot of you will probably recognize it
I modified it to look like this
and it works pretty good but it wants to return none
so how can I stop it from doing that

mport win32api

propname = 'FileDescription'


def get_file_description(windows_exe):
    language, codepage = win32api.GetFileVersionInfo(windows_exe, '\\VarFileInfo\\Translation')[0]
    stringfileinfo = u'\\StringFileInfo\\%04X%04X\\%s' % (language, codepage, 'FileDescription')
    stringfileinfo2 = u'\\StringFileInfo\\%04X%04X\\%s' % (language, codepage, 'FileVersion')
    description = win32api.GetFileVersionInfo(windows_exe, stringfileinfo)
    description2 = win32api.GetFileVersionInfo(windows_exe, stringfileinfo2)
    print('Name:', description)
    print('Version:', description2)


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

path = r'\\MyCloudPR4100\Programs\Specialty tools'
# program = r'\ccsetup551.exe'  # ccsetup551.exe
print(get_file_description(path + file4))
I have been using CCleaner for this experiment hears what it returns

Quote:Name: CCleaner Installer
Version: 5.51.0.6939
None

So how do I eliminate the none
any help would be appreciated
functions, unless told to do otherwise will return None.
Place a return statement in your code to return what you wish.

for example:
return description2
(Dec-13-2023, 02:36 AM)Larz60+ Wrote: [ -> ]functions, unless told to do otherwise will return None.
Place a return statement in your code to return what you wish.

for example:
return description2
okay I tried that method and it does work it does remove none
but I wind up with this

Name: CCleaner Installer
Version: 5.51.0.6939
5.51.0.6939
I would like to have the word version: in front of it
The answer is the same, use return.
return description, description2
And go work through some python tutorials. Using the forum is an inefficient way for you to learn python basics. We're happy to answer questions, but you must get tired of waiting for replies to your post.
(Dec-13-2023, 03:45 PM)deanhystad Wrote: [ -> ]The answer is the same, use return.
return description, description2
And go work through some python tutorials. Using the forum is an inefficient way for you to learn python basics. We're happy to answer questions, but you must get tired of waiting for replies to your post.

Quote:yes that's great but it doesn't give me what I need I want the name and the version in front
and by using those methods it puts the two values in brackets I'm trying to do it as follows
name:
version:
but the last line always ends up with none
(Dec-13-2023, 04:00 PM)Raysz Wrote: [ -> ][quote="deanhystad" pid='175182' dateline='1702482355']
The answer is the same, use return.
return description, description2
And go work through some python tutorials. Using the forum is an inefficient way for you to learn python basics. We're happy to answer questions, but you must get tired of waiting for replies to your post.

Quote:yes that's great but it doesn't give me what I need I want the name and the version in front
and by using those methods it puts the two values in brackets I'm trying to do it as follows
name:
version:
but the last line always ends up with none
hey everyone I just found the solution
at the end of
name:
version:
return ' ' <----- I put this in their
and that worked

Thanks to everyone I appreciate your help in solving my issue
That is not a solution. if your function is supposed to return information to the program, it needs to have a return statement. If a function is only used for a side effect, like printing something, it should not have a return statement. If your program is only calling get_file_description() to print the file name and version, to not return a value, and do not print the return value of the function. If you plan to use the file name or version information in your program, the function should return those values AND NOT PRINT ANYTHING.

You do not need to call print() to make the get_file_description(path + file4) function print. If you don't want to print the return value of the function, just call the function without printing.
path = r'\\MyCloudPR4100\Programs\Specialty tools'
get_file_description(path + file4)
If your program needs the information collected in get_file_description, Remove the print statements inside get_file_description and return the values.
from win32api import GetFileVersionInfo


def get_file_description(file):
    language, codepage = GetFileVersionInfo(file, "\\VarFileInfo\\Translation")[0]

    # Use newer formatting.  "%" is ancient.  f"strings became available in python 3.6
    prefix = f"\\StringFileInfo\\{language:04X}{codepage:04X}\\"
    return (
        GetFileVersionInfo(file, f"{prefix}\\FileDescription"),
        GetFileVersionInfo(file, f"{prefix}\\FileVersion"),
    )


# When using with open() the file closes automatically
with open("Program_Recovery2_Selection.txt", "r") as file:
    file4 = file.read().strip()

# Use / instead of \ as delimiter in path.  Both work, but
# \ causes problems because it also signifies the start of
# an escape sequence.
path = "/MyCloudPR4100/Programs/Specialty tools"
description, version = get_file_description(path + file4)
print("Name:", description)
print("Version:", version)
Your program should also handle exceptions that may occur. Not all executables have version information, so calling win32api.GetFileVersionInfo() can raise a pywintypes.error. Your program should catch these exceptions and return an informative message instead of crashing.
Quote:
(Dec-13-2023, 08:29 PM)deanhystad Wrote: [ -> ]That is not a solution. if your function is supposed to return information to the program, it needs to have a return statement. If a function is only used for a side effect, like printing something, it should not have a return statement. If your program is only calling get_file_description() to print the file name and version, to not return a value, and do not print the return value of the function. If you plan to use the file name or version information in your program, the function should return those values AND NOT PRINT ANYTHING.

You do not need to call print() to make the get_file_description(path + file4) function print. If you don't want to print the return value of the function, just call the function without printing.
path = r'\\MyCloudPR4100\Programs\Specialty tools'
get_file_description(path + file4)
If your program needs the information collected in get_file_description, Remove the print statements inside get_file_description and return the values.
from win32api import GetFileVersionInfo


def get_file_description(file):
    language, codepage = GetFileVersionInfo(file, "\\VarFileInfo\\Translation")[0]

    # Use newer formatting.  "%" is ancient.  f"strings became available in python 3.6
    prefix = f"\\StringFileInfo\\{language:04X}{codepage:04X}\\"
    return (
        GetFileVersionInfo(file, f"{prefix}\\FileDescription"),
        GetFileVersionInfo(file, f"{prefix}\\FileVersion"),
    )


# When using with open() the file closes automatically
with open("Program_Recovery2_Selection.txt", "r") as file:
    file4 = file.read().strip()

# Use / instead of \ as delimiter in path.  Both work, but
# \ causes problems because it also signifies the start of
# an escape sequence.
path = "/MyCloudPR4100/Programs/Specialty tools"
description, version = get_file_description(path + file4)
print("Name:", description)
print("Version:", version)
Your program should also handle exceptions that may occur. Not all executables have version information, so calling win32api.GetFileVersionInfo() can raise a pywintypes.error. Your program should catch these exceptions and return an informative message instead of crashing.

Quote:thank you for your input I will give it a try