Entering edit mode
4.5 years ago
faisala85
•
0
Hi, I'm new to Biopython and programming. I installed Biopython and Numpy on my Mac. I think I have installed it correctly since when I do some of the basic commands in the help document it returns the expected hits. However, when I run
from Bio import SeqIO
for seq_record in SeqIO.parse("ls_orchid.gbk", "genbank"):
printseq_record.id)
print(repr(seq_record.seq))
print(len(seq_record))
I get the error:
Traceback (most recent call last):
File "/Users/faisalahmad/Documents/test2.py", line 3, in <module>
for seq_record in SeqIO.parse("ls_orchid.gbk", "genbank"):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/biopython-1.77-py3.8-macosx-10.9-x86_64.egg/Bio/SeqIO/__init__.py", line 627, in parse
i = iterator_generator(handle)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/biopython-1.77-py3.8-macosx-10.9-x86_64.egg/Bio/SeqIO/InsdcIO.py", line 94, in __init__
super().__init__(source, mode="t", fmt="GenBank")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/biopython-1.77-py3.8-macosx-10.9-x86_64.egg/Bio/SeqIO/Interfaces.py", line 42, in __init__
self.stream = open(source, "r" + mode)
FileNotFoundError: [Errno 2] No such file or directory: 'ls_orchid.gbk'
How do I fix this?
There is your error, the file cannot be found in the directory you're in.
Thanks for the help. I'm using Python 3.8 IDLE. I have "ls_orchid.gbk" saved in the same directory as the script. Is there another program I should be using that is easier? How do I get it to read the file in the directory?
You still have to tell any program that you use where the file is. The $HOME directory is the default working directory unless you change it. I suggest you read tutorials on the very basics of python (or programming in general), this is not python-specific.
It is not enough to have the script and file in the same directory if your working directory is different. You will need to be inside the same folder.
If in doubt, use the full path to the file,
not just the file, use full path to everything :-)
I suggest to you the use of argparse to avoid manual insertion of input files (https://docs.python.org/3/library/argparse.html).
You can easily to this:
In this way, you just call the script and the file:
You also can use de sys.argv but with argparse you can pass well-documented help messages and set the arguments in any order, as well as you do not need the put the file in the same directory of your script, just pass the path to the desired file.
How does this address the file not found error? If the file does not exist, this method will produce the same error. I'm moving this answer to a comment - I'll move it back to an answer if you edit it and address the underlying problem.
by the logics, if faisala85 can pass the path and the file name, the file exists...
Not true. I can pass a made up file path and file name to a script. That doesn't prove the existence of an equivalent file at all.