0

I have recently tried to make a python code which takes a path of a file without an extension and determine what extension it has.

I was looking for something like the example below. In the example the extension is exe (but the code doesn't know that yet).

path = 'C:\\MyPath\\Example'
#takes the path above and guesses the programs extension:
extension = guess_extension(path)
#adds the extension to the path:
fullPath = path+extension
print(fullPath)

Output:

C:\MyPath\Example.exe

If you know a python module that would do that (or something similar), please list it below.


I have tried to use filetype (filetype.guess()) and mimetypes (mimetypes.guess_extension()) modules, but they would both return value of none.
I have also tried to use answers from many questions like this one, but that still didn't work.

Iland
  • 3
  • 3
  • Does this answer your question? [How to check type of files without extensions in python?](https://stackoverflow.com/questions/10937350/how-to-check-type-of-files-without-extensions-in-python) – Mike Poole Oct 01 '20 at 08:15
  • Do you mean "guess" just by name? Or do you want to actually *check* from the file itself? – Sentry Oct 01 '20 at 08:16
  • Your question is too vague. If this answer https://stackoverflow.com/questions/10937350/how-to-check-type-of-files-without-extensions-in-python doesn't help you, you have to look at which files are not correctly identified and frame a question around that particular problem. – Perry Oct 01 '20 at 08:20
  • Answer to Mike Poole and Perry: No, that doesn't answer my question. I said filetype module didn't work for me and that is the answer to that question. – Iland Oct 01 '20 at 08:24
  • Do you mean you know the partial name of the file (i.e. extension unknown). So in your example, you would want to find all files named "Example.*" (where * is wildcard)which would turn out to be Example.exe. Is this correct ? – DarrylG Oct 01 '20 at 08:26
  • Reply to DarryIG: I want to take a single file named Example which is the only one named so in the dir, and check what extension it has based on something. – Iland Oct 01 '20 at 08:28

1 Answers1

0

It sounds like the built in glob module (glob docs) might be what you're looking for. This module provides Unix style pattern expansion functionality within Python.

In the following example the incomplete path variable has the str .* appended to it when passed to glob.glob. This essentially tells glob.glob to return a list of valid paths found within the host system that start the same as path, followed by a period (designating a file extension), with the asterisk matching any and all characters following those from path + '.'.

import glob

path = r'C:\Program Files\Firefox Developer Edition\minidump-analyzer'

full = glob.glob(path+'.*')

print(full[0])

Output: C:\Program Files\Firefox Developer Edition\minidump-analyzer.exe

It is worth noting that the above is just an illustration of how glob could be leveraged as part of a solution to your question. Proper handling of unexpected inputs, edge cases, exceptions etc. should be implemented as required by the needs of your program.

JPI93
  • 1,507
  • 5
  • 10