How to get the file name to be opened with my program by clicking on this file?

Asked

Viewed 186 times

-1

I created a music player in the Python language and I would like to know how to play a song with my program by clicking on the file. For example when I open a song now, it automatically plays on Windows Media Player.

They told me I can get the path from where the script is called with sys.argv thus:

import sys
import os

for arg in sys.argv:
    print(arg)

But I would also like to get the file path that is opened with my program. When I run my code, the only thing I get from the list is the script path:

["C:\Users\Username\Desktop\Nova pasta\script.py"]

How can I play songs with my program as well as in WMP?

  • 1

    I honestly don’t understand your question. If your question is about how to open a song with your player always, you will first have to generate an executable of your code. After that click on Open with > Choose default program and choose your program. After that, whenever you open a song, your program will run and the absolute path of that song will be passed on sys.argv.

  • 1

    It’s hard to ask because you don’t have a clear idea of what you need. See this lib: https://docs.python.org/3.3/library/argparse.html

  • Yes of all this I know, but as the same compiled me program will recognize the file, as it will have the file path if it can not because I do not know how....

  • 2

    Phoenix as I said before, when opening a song with a program the music directory is passed on sys.argv. The library argparse that Augusto mentioned is a parser to improve the form that is placed and receives the data entered in the command line in the execution of the program.

  • 2

    Phoenix, here is an example of how you get the music file to play in your program. https://repl.it/repls/SingleMuffledCell Only generate an executable of this code and follow the steps I showed in my first comment.

1 answer

1


Good as now the question became a little clearer and specific, now you can already answer.

As I said in the comments, to play a music file or any other file on Windows using your program, you will first have to generate a code executable.

After generating the executable, you will need to set your program as default so that it can always be run while playing the desired file type.

To do this, just follow the steps below:

  • Right-click the desired file.
  • Click "Open With" > "Choose Default Program..." > "Search...".
  • Select the executable of your program.
  • Let the box "Always use selected program to open this file type" marked.
  • Click "OK" to make the changes.

"Ok, I set everything up as ordered. But how will I make my program get this file ?"

When you double-click the file to open, Windows internally runs a program set as default to play the file.

When running the program, Windows passes on the command line let’s say so (not sure how it works internally), the absolute path of the file clicked by the user.

You can get the file path in the Python language through the list sys.argv. This list stores all data entered when running the program on the command line.

See the example below:

import sys

class YourPlayer(object):
    """
    Simulação do seu player que vai reproduzir a música.
    """
    def __init__(self, sound):
        self.__sound = sound

    def run(self):
        print("Playing " + self.__sound)

# Recebe o caminho absoluto do arquivo de música.
sound_filename = sys.argv[1] 
player = YourPlayer(sound_filename)

# Executa o seu player.
player.run() 
input()      # Esse input é só para não terminar o programa.

See how it works online: https://repl.it/repls/SingleMuffledCell

"But when I run the program, I get in the list only the path of the executed script."

If this is happening, it’s for two reasons. The first reason is that you don’t seem to understand the part where I talk that you need to generate an executable from your code. What needs to be on the list first is not the script path, but the executable path.

The second reason is because you’re opening your own program. What you should do is double click on the file to play to open the file with your program.

You can also directly run your program with a filename on the command line. Example:

> MyPlayer "minha_musica.mp3"

Only to complement, in respect of argparse that Augusto mentioned in the comments, this is a library for the purpose of parsing the data entered in the command line.

How is that not a question about argparse I won’t explain here how it works, but you can easily learn the basics of this library on this website (that’s where I learned).

Below is an example of how this library works:

from argparse import ArgumentParser

parser = ArgumentParser(description = "Um programa de exemplo")
parser.add_argument("--file", help = "Arquivo a ser reproduzido", required = True)
parser.add_argument("--vol", help = "Volume inicial do áudio", type = int, default = 60)

arguments = parser.parse_args()
print("Reproduzindo", arguments.file, "no volume", str(arguments.vol) + "%")

Running the code:

> script.py --file="minha_musica.mp3"
Reproduzindo minha_musica.mp3 no volume 60%

> script.py --file="minha_musica.mp3" --vol=97
Reproduzindo minha_musica.mp3 no volume 97%

> script.py
usage: script.py [-h] --file FILE [--vol VOL]
script.py: error: the following arguments are required: --file

As you can see, through the argparse we can insert the arguments in a more elegant way, define parameters as mandatory, define the type of the value of each parameter, define a default value for the parameter, get a help message, among other things.

I hope I’ve helped you :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.