As the question has now become a little clearer and specifies, now we can respond.As I said in the comments, to play a music file or any other file on Windows using your program, first you will have to generate a code executable.After you have generated the executable, you will need to configure your program as default so that it can always run when playing the desired file type. To do this, just follow the steps below:Right-click the desired file.Click "Open with" > "Choose default program..." > "Browse..."Select the executable of your program.Leave the box "Always use the selected program to open this file type" marked.Click "OK" to make the changes."Okay, I set it up as you did. But how will I make my program get this file?"When you double-click the file to open, Windows internally runs a program set to default to play the file. When running the program, Windows passes on the command line let's say so (I don't know how it works internally), the absolute path of the file clicked by the user.You can get the file path in Python language through the list sys.argv. This list stores all the 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 worked online: https://repl.it/repls/SingleMuffledCell " But when I run the program, I get in the list only the path of the script run. "If this is happening it is for two reasons. The first reason is that you don't seem to have understood the part where I say you need to generate an executable of your code. What needs to be on the list first is not the script path and yes the executable.The second reason is because you are opening your program alone. What you should do is double-click the file to be played to open the file with your program. You can also directly run your program with a file name on the command line. Example:> MyPlayer "minha_musica.mp3"
Only to complement, with respect to argparse that Augusto mentioned in the comments, this is a library with the aim of parsing the data entered in the command line.How this is not a question about argparse I will not explain here how it works, but you can easily learn the basics of this library https://cadernodelaboratorio.com.br/2017/06/05/python-3-processando-argumentos-da-linha-de-comando/ (it was 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 argparse we can insert the arguments in a more elegant way, set parameters as mandatory, set the value type of each parameter, set a default value for the parameter, get a help message, among other things more.I hope I helped you:)