return none - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: return none (/thread-41283.html) |
return none - Raysz - Dec-13-2023 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 So how do I eliminate the none any help would be appreciated RE: return none - Larz60+ - Dec-13-2023 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 RE: return none - Raysz - Dec-13-2023 (Dec-13-2023, 02:36 AM)Larz60+ Wrote: functions, unless told to do otherwise will return None. RE: return none - deanhystad - Dec-13-2023 The answer is the same, use return. return description, description2And 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. RE: return none - Raysz - Dec-13-2023 (Dec-13-2023, 03:45 PM)deanhystad Wrote: The answer is the same, use return. Quote:yes that's great but it doesn't give me what I need I want the name and the version in front RE: return none - Raysz - Dec-13-2023 (Dec-13-2023, 04:00 PM)Raysz Wrote: [quote="deanhystad" pid='175182' dateline='1702482355'] Quote:yes that's great but it doesn't give me what I need I want the name and the version in fronthey 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 RE: return none - deanhystad - Dec-13-2023 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. RE: return none - Raysz - Dec-15-2023 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. Quote:thank you for your input I will give it a try |