检查JSON文件是否在另一个过程中运行/使用,并在block acces/运行(如果是)

发布于 2025-02-07 19:06:17 字数 1838 浏览 0 评论 0原文

我要您的帮助。如果已经打开并使用了JSON文件,我想阻止访问和打开。 我正在通过两次运行代码并两次打开同一文件来测试。我希望如果没有打开,一开始什么都不做。之后,我会添加一个qdialog来警告用户

我的灵感来自帖子”检查是否未打开文件,也没有由另一个过程使用

,但它根本没有帮助我阻止访问权限。从我从帖子中理解的内容来看,只能通过打印警告文件已经在进行

    self.name, _filtre = QtWidgets.QFileDialog.getOpenFileName(self.centralwidget, filter='*.json')


        with open(self.name) as file:
            for file in psutil.process_iter():
                    flist = file.open_files()
#if my file is already running, block acces
                    if flist:
                        raise FileExistsError

                    else :
                        self.dataTab = []
                        # Lecture du fichier
                        self.jsonlist = json.load(file)
                        self.site = self.jsonlist["site"]
                        self.user = self.jsonlist["user"]
                        self.project = self.jsonlist["project"]
                        self.dt_string = self.jsonlist["Date-Time"]

                        # dataTab récupère les valeurs dans la liste de la clé 'Test'
                        self.dataTab = self.jsonlist['Test']

我的错误:

Connected to pydev debugger (build 201.8743.20)
Traceback (most recent call last):
  File "C:\Users\station\PycharmProjects\asa_control\BAA_TESUT_ajoutASA.py", line 637, in open_project
    self.file_open()
  File "C:\Users\station\PycharmProjects\asa_control\BAA_TESUT_ajoutASA.py", line 661, in file_open
    self.jsonlist = json.load(file)
  File "C:\Users\station\anaconda3\envs\asa_station\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
AttributeError: 'Process' object has no attribute 'read'

I am asking for your help. I would like to block the access and opening of a json file if it is already open and in use.
I am testing by running my code twice and opening the same file twice. I would like that if one is not opened, at first that it does nothing. Afterwards, I would add a QDialog to warn the user

I was inspired by a post Check if a file is not open nor being used by another process

but it doesn't help me at all to block the access. From what I understood from the post, it's only to warn by a print that a file is already in progress

    self.name, _filtre = QtWidgets.QFileDialog.getOpenFileName(self.centralwidget, filter='*.json')


        with open(self.name) as file:
            for file in psutil.process_iter():
                    flist = file.open_files()
#if my file is already running, block acces
                    if flist:
                        raise FileExistsError

                    else :
                        self.dataTab = []
                        # Lecture du fichier
                        self.jsonlist = json.load(file)
                        self.site = self.jsonlist["site"]
                        self.user = self.jsonlist["user"]
                        self.project = self.jsonlist["project"]
                        self.dt_string = self.jsonlist["Date-Time"]

                        # dataTab récupère les valeurs dans la liste de la clé 'Test'
                        self.dataTab = self.jsonlist['Test']

my error :

Connected to pydev debugger (build 201.8743.20)
Traceback (most recent call last):
  File "C:\Users\station\PycharmProjects\asa_control\BAA_TESUT_ajoutASA.py", line 637, in open_project
    self.file_open()
  File "C:\Users\station\PycharmProjects\asa_control\BAA_TESUT_ajoutASA.py", line 661, in file_open
    self.jsonlist = json.load(file)
  File "C:\Users\station\anaconda3\envs\asa_station\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
AttributeError: 'Process' object has no attribute 'read'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文