Using command line arguments to open files with PyQt5 apps -- Windows file associations

I developed a simple video player and packed it with pyinstaller. After installation, if I click on the .MP4 file, how to open it with my player? I try to open the. MP4 file. First, I want to get the file name and path, but I don’t know how to do it. Can you help me think about it?
35

I’m not sure that I completely understood what you want to do.

However, if you are on windows and you want to add your program to the open with list, try to read this:

Of course, in your case you should use the .mp4 extension instead of the one in the article.

If you don’t want to manually change the windows registry try to see if the following utility could be helpful:

https://defaultprogramseditor.com/

Hey @qinhong_lai welcome to the forum! As @Luca says you can set your application as the “file handler” for a given file type.

You can set this up by right-clicking on a file in Explorer, selecting Open with… and then “Choose another app”.

In the window that opens you’ll see applications that are installed on your computer. If you don’t see your application here, keep scrolling down and you’ll see “Look for another app on this PC” and then you can select your app by EXE file.

When Windows tries to open the file with your application, it will pass the filename as an argument to your app – the filename will be available in sys.argv. The following app example will display all the command line arguments when run.

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout

import sys


class Window(QWidget):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()

        for arg in sys.argv:  # <1> sys.argv is a list of strings.
            l = QLabel(arg)
            layout.addWidget(l)

        self.setLayout(layout)
        self.setWindowTitle("Arguments")


app = QApplication(sys.argv)
w = Window()
w.show()

app.exec_()

If you run this with python file.py <filename to open> the file.py will be the first argument in sys.argv. What happens in a packaged app will depend on how it’s packaged.

For example,

python arguments.py filename.mp4

Gives the following window

arguments

For opening a file, you could also use sys.argv[-1] to get the last argument (if you’re not passing anything else). Or alternatively, remove the current script filename from the list using the following

if __file__ in sys.argv:
    sys.argv.remove(__file__)

Thank you. You have successfully solved this problem and obtained the file path and name. Now I have another problem

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()

        for arg in sys.argv:  # <1> sys.argv is a list of strings.
            l = QLabel(arg)
            layout.addWidget(l)

        self.setWindowIcon(QIcon('resource/logo.ico'))
        self.setLayout(layout)
        self.setWindowTitle("Arguments")

app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()

I want to add a program icon
self.setWindowIcon(QIcon(‘resource/logo.ico’))
It can be displayed normally

But I use pyinstaller to package and generate exe
pyinstaller -w test20.py

When the .MP4 video file on the desktop of the computer is opened through test20.exe, it will not be normal displayed. That’s why I can only copy one picture to the desktop,This is not the way to do it
43

Thank you. You have successfully solved this problem and obtained the file path and name. Now I have another problem

A post was split to a new topic: How to specify absolute paths for QIcon