-2

I keep receiving this error when I try to do anything in Biopython. I am not sure how to change the path of Biopython since python and Biopython are in the same path. Im not sure what else I need to do.

    Python 3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27) 
    [Clang 6.0 (clang-600.0.57)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from Bio import SeqIO
    >>> 
    >>> user_input = input("please input an Accession Number: ")
    please input an Accession Number: NC_005816.gb

    >>> file = SeqIO.read(user_input, "genbank")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-       packages/Bio/SeqIO/__init__.py", line 654, in read
        iterator = parse(handle, format, alphabet)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-  packages/Bio/SeqIO/__init__.py", line 607, in parse
        return iterator_generator(handle)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site- packages/Bio/SeqIO/InsdcIO.py", line 93, in __init__
        super().__init__(source, mode="t", fmt="GenBank")
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/Bio/SeqIO/Interfaces.py", line 47, in __init__
        self.stream = open(source, "r" + mode)
    FileNotFoundError: [Errno 2] No such file or directory: 'NC_005816.gb'

Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
Chris
  • 59
  • 1
  • 9
  • 2
    `NC_005816.gb` is not in the directory you invoked `python` in. Try doing `import os; os.chdir("path/to/NC_005816.gb")` to change the current working directory – theX Sep 22 '20 at 16:09
  • Why do you think this is related to BioPython? The error is very clear. There is no such file `NC_005816.gb` (which is the input you gave) in the current working directory – Tomerikoo Sep 22 '20 at 16:10
  • Are you trying to look up the genbank file from the accession without have a file stored locally? If yes, you need `Bio.Entrez` http://biopython.org/DIST/docs/tutorial/Tutorial.html#sec139 – Chris_Rands Sep 24 '20 at 16:36

1 Answers1

1

NC_005816.gb is not in the directory you invoked python in. Try doing

import os
os.chdir("path/to/folder/of/NC_005816.gb")
...  # Your code here

to change the current working directory. But if you need to change dynamically towards user input, you can ask for the path to the file instead:

from pathlib import Path
from Bio import SeqIO
user_input = input("Enter the path to the accession number:")

if Path(user_input).expand_user().exists():
    file = SeqIO.read(user_input, "genbank")
else:
    print("Invalid input")

Python's pathlib docs can be found here

theX
  • 1,008
  • 7
  • 22