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

时间:2022-09-06 21:09:12

I am programming a web based application in Python and PHP. Now I running the server on the same computer as I am programming. The Python script will be executed in PHP. This part is working well.

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

The script have to read files and store pathnames in a list. This list is used in the rest ot the script.

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

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']

The function readFiles will use this list. fileList is a global variable. Its made by glob.

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

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

When running on Apache when I call the Python script I get the following error in tail -f /var/log/apache2/error.log :

当我在调用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'

When I run the same script in IDLE and Linux Terminal the script gives no errors and the correct visualisation of the content of the file(s).

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

I tried already CHMOD 777 in the directory where the script have to read the files and check with ls -l the files have all permissions. It's done for my own account and the account of the Apache server. I am administrator on this Linux computer.

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

In the orignal script, fileList is created in the script itself. I also needed to create the list in IDLE and copy/paste to the same script thats running on Apache. Without this on Apache [] is returned only and in IDLE the correct list that is shown above. This may caused by the same problem but there is no error message in this case. When I put the correct list in the script on Apache, the error occured.

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

Why does the script work in IDLE and directly in command line correctly and on Apache it dont have the permission to open files, even with CHMOD 777 on my account and the account of the server?

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

2 个解决方案

#1


1  

It seems to me that in your fileList variable there is a space after Python-modules in the path of B.txt.

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

/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/B.txt

Try to remove it and check once.

尝试删除它并检查一次。

#2


0  

Thank you for the tips but it didn't help. I found a dirty way to solve the problem. While putting the files in /tmp/ folder I don't have the 'Permission denied' error anymore.

谢谢你的提示,但它没有帮助。我找到了一种解决问题的方法。将文件放在/ tmp /文件夹中时,我不再有“权限被拒绝”错误。

#1


1  

It seems to me that in your fileList variable there is a space after Python-modules in the path of B.txt.

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

/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/B.txt

Try to remove it and check once.

尝试删除它并检查一次。

#2


0  

Thank you for the tips but it didn't help. I found a dirty way to solve the problem. While putting the files in /tmp/ folder I don't have the 'Permission denied' error anymore.

谢谢你的提示,但它没有帮助。我找到了一种解决问题的方法。将文件放在/ tmp /文件夹中时,我不再有“权限被拒绝”错误。