在 Apache2 上运行 Python 脚本时,我收到此错误:IOError:[Errno 13] 权限被拒绝


While running a Python script on Apache2, I get this error: IOError: [Errno 13] Permission denied

我正在用Python和PHP编写一个基于Web的应用程序。现在,我在编程的同一台计算机上运行服务器。Python 脚本将以 PHP 执行。这部分运行良好。

脚本必须读取文件并将路径名存储在列表中。此列表用于脚本的其余部分。

fileList = ['/home/mark/Bureaublad/Dropbox/Stage-documenten/Python-modules /Read_files_determine_overlap_and_visualisation/B.txt', '/home/mark/Bureaublad/Dropbox/Stage-documenten/Python-modules/Read_files_determine_overlap_and_visualisation/D.txt', '/home/mark/Bureaublad/Dropbox/Stage-documenten/Python-modules/Read_files_determine_overlap_and_visualisation/C.txt', '/home/mark/Bureaublad/Dropbox/Stage-documenten/Python-modules/Read_files_determine_overlap_and_visualisation/E.txt', '/home/mark/Bureaublad/Dropbox/Stage-documenten/Python-modules/Read_files_determine_overlap_and_visualisation/A.txt']

函数 readFiles 将使用此列表。 fileList 是一个全局变量。它是由全球制造的。

def readFiles():
Dic = {}
for filePath in fileList:
    geneList = [] #create a new list every new loop to put in genesPerBacteriaDic as value
    for line in open(filePath, 'r').readlines(): #innerloop to read every file in lines
        if not line.startswith('FIG'):
            sys.exit('Wrong file format!!'nUpload FIGFAMS-format only') #terminate program if incorrect file format is imported
        FIGnumber = line[0:11] #remove ''n' or another characters on the end of the string
        geneList.append(FIGnumber)
    head, fileName = os.path.split(filePath) # convert filePath to fileName
    Dic[fileName] = geneList
return Dic

当我调用 Python 脚本时,在 Apache 上运行时,我在 tail -f/var/log/apache2/error 中收到以下错误.log:

Traceback (most recent call last):
File "/var/www/Coregrapher/makeVisualisation.py", line 192, in <module>
main()
File "/var/www/Coregrapher/makeVisualisation.py", line 48, in main
genesPerBacteriaDic = readFiles()
File "/var/www/Coregrapher/makeVisualisation.py", line 70, in readFiles
for line in open(filePath, 'r').readlines(): #innerloop to read every file in lines
IOError: [Errno 13] Permission denied: '/home/mark/Bureaublad/Dropbox/Stage-documenten/Python-modules/Readp_files_determine_overlap_and_visualisation/B.txt'

当我在 IDLE 和 Linux 终端中运行相同的脚本时,脚本不会给出任何错误,并且可以正确可视化文件的内容。

我已经在脚本必须读取文件并使用 ls -l 检查文件是否具有所有权限的目录中尝试了 CHMOD 777。它是为我自己的帐户和Apache服务器的帐户完成的。我是这台 Linux 计算机上的管理员。

在原始脚本中,fileList 是在脚本本身中创建的。我还需要在 IDLE 中创建列表,并将复制/粘贴到在 Apache 上运行的同一脚本中。如果没有这个,Apache [] 只返回,在 IDLE 中返回上面显示的正确列表。这可能是由相同的问题引起的,但在这种情况下没有错误消息。当我在 Apache 上的脚本中输入正确的列表时,发生了错误。

为什么该脚本在 IDLE 中和直接在命令行中正确工作,并且在 Apache 上它没有打开文件的权限,即使在我的帐户和服务器帐户上使用 CHMOD 777 也是如此?

在我看来

,在您的 fileList 变量中,在 B.txt 路径中的 Python 模块之后有一个空格。

/

home/mark/Bureaublad/Dropbox/Stage-documenten/Python-modules/Read_files_determine_overlap_and_visualisation/B.txt

尝试将其删除并检查一次。

感谢您的提示,但没有帮助。我找到了解决问题的肮脏方法。将文件放在/tmp/文件夹中时,我不再出现"权限被拒绝"错误。