0

我正在用 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 是一个全局变量。它是由 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

在 Apache 上运行时,当我调用 Python 脚本时,在 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 终端中运行相同的脚本时,该脚本没有给出错误并且文件内容的正确可视化。

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

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

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

4

2 回答 2

1

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

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

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

于 2013-10-03T13:32:52.140 回答
0

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

于 2013-10-03T12:01:05.360 回答