0

So I am trying to create a program that would be able to read its own types of files

Imagine there is a file called hello.world

And you have a .exe program that you can open this file with.

This program is my python file, so what is actually the way of getting which file was opened with that .exe?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Nitlix
  • 13
  • 4
  • I heard you can use something like argv in the beginning of the program, but I am not sure – Nitlix Mar 29 '22 at 14:18
  • .exe is created afterwards, you create the algorithm then you pack it to have an exe , it's not the first step , or do you have it ready? – Ran A Mar 29 '22 at 14:18
  • do you want to get the name of the file called? – Guy Nachshon Mar 29 '22 at 14:18
  • https://stackoverflow.com/questions/3324372/finding-the-app-window-currently-in-focus-on-mac-osx – Ran A Mar 29 '22 at 14:21
  • @RanA , I would make the script, pack it into a .exe and then try to open the file with it. The only thing I am not sure about is how to get what file/directory of the file inside the program. Or if I am correct? xD – Nitlix Mar 29 '22 at 14:25
  • it's a bit unclear, the script do exactly what you program it to do , so if you program it to open a file you already have that information – Ran A Mar 29 '22 at 14:31
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 29 '22 at 14:45
  • @RanA , and so I am asking about how to get that info :) About how to get the full directory of the file opened with a .exe or the contents of the file iself. – Nitlix Mar 29 '22 at 14:47
  • @Nitlix It seems like your code have not been written yet and you are still brainstorming, which is good. The thing is, when creating the script (before packing it into an .exe), in order for the script to work, you already need to supply it with a file name (through `argv` or of similar fashion). Therefore, the "inner script" already has that info - your packed .exe should not need any additional information on the file again. This may illustrate how to get your Python script to work with file: https://stackoverflow.com/questions/14360389/getting-file-path-from-command-line-argument-in-python – Bao Huynh Lam Mar 29 '22 at 15:29

1 Answers1

0

After a small discussion in the comments:

it is possible to use sys.argv to get the file path: getting file path from command line argument in python

filename = sys.argv[1]
print(filename)

if os.path.exists(fn):
    print os.path.basename(fn)
    # file exists
Nitlix
  • 13
  • 4