linux 权限错误:[Errno 13]在虚拟机上运行文件但在本地计算机上运行正常时权限被拒绝

ca1c2owp  于 2023-01-16  发布在  Linux
关注(0)|答案(1)|浏览(275)

正如标题所示,我正在运行一个python程序,无论何时我在本地机器上运行它,它都会正确运行程序,没有错误。但是当我在学校指定的虚拟机上运行它时,它会抛出一个错误。请记住,程序正在阅读的文件有200万行长,而在我的本地机器上,输出文件有200k+行长。在虚拟机上,程序在大约23k行之后崩溃。

import os

if os.path.exists("proteinSeq.txt"): # makes sure that text files don't overlaps
    os.remove("proteinSeq.txt")

def treat_DNA(seq): # a function for the DNA
#         print('The original DNA sequence is', seq)
      
        CompletmentDict = {'A':'T', 'T':'A', 'G':'C', 'C' : 'G', 'N':'N'} #dict for complement
        final = "" # used for combining all the letters together
        for letter in seq:    #runs the letters of the line through completment dictonary
            final += CompletmentDict[letter]  # combines new letters 
#         print ("Your completement is: ", final)
      
        final2 = ""    # used for combining all the letters together
        DNATORNADICT = {'A':'U', 'T':'A', 'G':'C', 'C' : 'G', 'N':'N'} 
        for letters in final: #runs the letters of the line through RNA dictonary
            final2 +=  DNATORNADICT[letters] # combines new letters 
#         print("Your Final DNA TO RNA TRANSCRIPTION IS: " + final2)
      
        rna2protein = {'UUU':'F', 'UUC':'F', 'UUA':'L', 'UUG':'L',
        'UCU':'S', 'UCC':'S', 'UCA':'S', 'UCG':'S',
        'UAU':'Y', 'UAC':'Y', 'UAA':'', 'UAG':'',
        'UGU':'C', 'UGC':'C', 'UGA':'', 'UGG':'W',
        'CUU':'L', 'CUC':'L', 'CUA':'L', 'CUG':'L',
        'CCU':'P', 'CCC':'P', 'CCA':'P', 'CCG':'P',
        'CAU':'H', 'CAC':'H', 'CAA':'Q', 'CAG':'Q',
        'CGU':'R', 'CGC':'R', 'CGA':'R', 'CGG':'R',
        'AUU':'I', 'AUC':'I', 'AUA':'I', 'AUG':'M',
        'ACU':'T', 'ACC':'T', 'ACA':'T', 'ACG':'T',
        'AAU':'N', 'AAC':'N', 'AAA':'K', 'AAG':'K',
        'AGU':'S', 'AGC':'S', 'AGA':'R', 'AGG':'R',
        'GUU':'V', 'GUC':'V', 'GUA':'V', 'GUG':'V',
        'GCU':'A', 'GCC':'A', 'GCA':'A', 'GCG':'A',
        'GAU':'D', 'GAC':'D', 'GAA':'E', 'GAG':'E',
        'GGU':'G', 'GGC':'G', 'GGA':'G', 'GGG':'G', 'UAA': 'STOP', 'UAG': 'STOP', 'UGA': 'STOP' }
        final3 = ""
        for p in range(0,len(final2),3): #character with length of final 2
            myKey = final2[p:p+3] # three RNA letters  
            if "N" in myKey: # if rna has N in it, turn it into X
                final3+="X"
            elif len(myKey) <= 2: # if the codon has less then two characters, ignore it.
                pass
            else:
#                 print("key", myKey) # prints out the three letters that will go in the dictonary.
#                 print(rna2protein.get(myKey)) # runts the three letters through the rna2protein dictonary.
                if rna2protein.get(myKey) == "STOP": # if the RNA gets translated into a protein that has the stop signal, it will stop traslating that strand of RNA.
                    final3 += "-"
                    # exit()
                else:
                    final3 += rna2protein.get(myKey) #combines the previously translate protein with the newly created ones.
#         print("Resulting protein is: ", final3)
      
        with open(r'/home/student/Desktop/classroom/myfiles/HomoSapienFASTAhomework/proteinSeq.txt', 'a') as file:
            file.write(final3+'\n') #writes protein sequence in file
    #exit function
file = open(r'/home/student/Desktop/classroom/myfiles/HomoSapienFASTAhomework/Homo_sapiens.GRCh38.cds.all.fa' , 'r') # opens dna file
    
DNASequence = ''
for line in file.readlines(): 
        if line.startswith('>'): #if line starts  with '>' , continue
            if DNASequence:
                treat_DNA(DNASequence) # runrs DNA sequences through the treat_DNA function made above
            DNASequence = ''
            with open(r'/home/student/Desktop/classroom/myfiles/HomoSapienFASTAhomework/proteinSeq.txt', 'a') as file:
                file.write(line) #writes lines inside proteinseq.txt
        else:
            DNASequence += line.strip() 
treat_DNA(DNASequence)

下面是我的代码。
并且存在两种类型的错误:这个

---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
<ipython-input-5-632b17e5ebb8> in <module>
     65             DNASequence = ''
     66             with open(r'/home/student/Desktop/classroom/myfiles/HomoSapienFASTAhomework/proteinSeq.txt', 'a') as file:
---> 67                 file.write(line) #writes lines inside proteinseq.txt
     68         else:
     69             DNASequence += line.strip()

PermissionError: [Errno 13] Permission denied

或者这个

---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
<ipython-input-6-632b17e5ebb8> in <module>
     62         if line.startswith('>'): #if line starts  with '>' , continue
     63             if DNASequence:
---> 64                 treat_DNA(DNASequence) # runrs DNA sequences through the treat_DNA function made above
     65             DNASequence = ''
     66             with open(r'/home/student/Desktop/classroom/myfiles/HomoSapienFASTAhomework/proteinSeq.txt', 'a') as file:

<ipython-input-6-632b17e5ebb8> in treat_DNA(seq)
     54 
     55         with open(r'/home/student/Desktop/classroom/myfiles/HomoSapienFASTAhomework/proteinSeq.txt', 'a') as file:
---> 56             file.write(final3+'\n') #writes protein sequence in file
     57     #exit function
     58 file = open(r'/home/student/Desktop/classroom/myfiles/HomoSapienFASTAhomework/Homo_sapiens.GRCh38.cds.all.fa' , 'r') # opens dna file

PermissionError: [Errno 13] Permission denied

我不认为有一种方法可以让我上传200万行的文件或输出文件从我的本地机器。
虚拟机运行在linux上,所以我尝试使用chmod来给予python权限(至少我认为chmod是这样做的)完全访问文件,但这也不起作用。

lndjwyie

lndjwyie1#

抱歉我忽略了PermissionError: [Errno 13] Permission denied
所以这不是由于内存不足,而是由于运行程序的用户没有访问VM上的文件或目录的权限。
要解决此问题,您可以尝试以下操作:

  • 确保运行该程序的用户具有访问文件或目录所需的权限
  • 使用ls -l命令检查文件和目录权限
  • 使用chmod命令更改文件权限
  • 使用sudo以具有必要权限的超级用户身份运行程序
  • 此外,请确保用于访问该文件的路径正确。
  • 此外,请确保文件存在,并检查其路径位置,该程序正在尝试访问文件在正确的位置或没有。
  • 请确保受影响的文件不是被其他进程锁定的文件。您可以使用lsoffuser命令进行检查:

要检查文件是否正被其他进程使用:
使用lsoflsof -c <process name> /path/to/file
使用fuserfuser -v /path/to/file

相关问题