在尝试以管理员身份写入文件时,为什么我在Python中收到权限错误?

问题描述 投票:0回答:1

我正在尝试写入Python目录中的文件。该文件也由另一个程序(MT4)编辑。当MT4程序未在该目录中执行任何操作时,该文件将成功写入。但是,每当运行MT4程序时,python程序都会抛出错误。

这是python错误:

PermissionError: [Errno 13] Permission denied: 'AppData\\Roaming\\MetaQuotes\\Terminal\\94DDB309C90B408373EFC53AC730F336\\MQL4\\Files\\modelout.txt'

python程序在这里:

from keras.models import load_model

import numpy
import os, time
import ctypes, sys

i = 0

global lastmoddate
lastmoddate = 5

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    while i > -1:
        newmoddate = os.stat("AppData\Roaming\MetaQuotes\Terminal\94DDB309C90B408373EFC53AC730F336\MQL4\Files\indicatorout.csv")[8]
        if newmoddate != lastmoddate:
                model = load_model('tried2.h5')
                data = numpy.loadtxt("AppData\Roaming\MetaQuotes\Terminal\94DDB309C90B408373EFC53AC730F336\MQL4\Files\indicatorout.csv", delimiter=",")
                data = numpy.array([data])
                print(data)
                outdata = model.predict(data)
                print(outdata)
                final = numpy.around(outdata, 0)
                numpy.savetxt("AppData\Roaming\MetaQuotes\Terminal\94DDB309C90B408373EFC53AC730F336\MQL4\Files\modelout.txt", final)
                time.sleep(15)
                lastmoddate = os.stat("AppData\Roaming\MetaQuotes\Terminal\94DDB309C90B408373EFC53AC730F336\MQL4\Files\indicatorout.csv")[8]
        else:
                time.sleep(5)
else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

我用一个解决方案从另一个堆栈溢出帖子中获取管理员。但是,我从来没有被提示我想象的意味着python已经以管理员身份运行。

如果有人可以就如何解决此错误提出任何建议,我们将不胜感激。谢谢!

python numpy mql4
1个回答
0
投票

Running cmd.exe as an administrator

由于在Windows中没有sudo命令,因此必须以管理员身份运行终端(cmd.exe)才能达到与sudo相当的权限级别。

Find cmd.exe in C:\Windows\system32
Right-click on it and Select Run as Administrator
It will then open the command prompt in the directory C:\Windows\system32
Go to your project directory
Execute Script
© www.soinside.com 2019 - 2024. All rights reserved.