Body: <p>I am a beginner and trying to read a bam file in Python.</p>
  <p>The lines below throw the error</p>
  <pre><code>OSError: [Errno 8] could not open alignment file `mybam.bam`: Exec format error
  </code></pre>
  <p>What could be the possible solution?</p>
  <pre><code>user@server:~/Documents$ python3
  Python 3.8.10 (default, Jun  2 2021, 10:49:15)
  [GCC 9.4.0] on linux
  Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  
  &gt;&gt;&gt; import pysam
  &gt;&gt;&gt; samfile = pysam.AlignmentFile(&quot;mybam.bam&quot;, &quot;rb&quot;)
  
  [E::hts_hopen] Failed to open file mybam.bam
  [E::hts_open_format] Failed to open file &quot;mybam.bam&quot; : Exec format error
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;pysam/libcalignmentfile.pyx&quot;, line 742, in pysam.libcalignmentfile.AlignmentFile.__cinit__
    File &quot;pysam/libcalignmentfile.pyx&quot;, line 941, in pysam.libcalignmentfile.AlignmentFile._open
  OSError: [Errno 8] could not open alignment file `mybam.bam`: Exec format error
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-15T18:25:31.143Z
Comments.Text: this is something related to the file not with python. Check the file with the linux command `file mybam.bam` just to know that you have a bam instead of a sam, text format. If you have a compressed file in bam format, check the head with `samtools view mybam.bam | head -100`, if this fails check the mapping software again where you got the alignment from.
Comments.UserDisplayName: zorbax
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-15T22:02:53.320Z
Comments.Text: https://www.biostars.org/p/284417/
Comments.UserDisplayName: Maximilian Press
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-16T09:54:17.300Z
Comments.Text: Thank you for the help. samtools view mybam.bam | head -100
  outputed a hundred lines, which have no index. As there is no index, I was able to retrieve the content with:
  for read in samfile.fetch(until_eof=True):
      print(read)
Comments.UserDisplayName: monade
Tags: <python><phylogenetics><sam><file-formats><pysam>
Title: pysam "Exec format" error
ThreadText: # pysam "Exec format" error
  
  (Asked by: username_0 on 2021-10-15)
  
  I am a beginner and trying to read a bam file in Python.
  
  The lines below throw the error
  
  ```
  OSError: [Errno 8] could not open alignment file `mybam.bam`: Exec format error
  
  ```
  
  What could be the possible solution?
  
  ```
  user@server:~/Documents$ python3
  Python 3.8.10 (default, Jun  2 2021, 10:49:15)
  [GCC 9.4.0] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  
  >>> import pysam
  >>> samfile = pysam.AlignmentFile("mybam.bam", "rb")
  
  [E::hts_hopen] Failed to open file mybam.bam
  [E::hts_open_format] Failed to open file "mybam.bam" : Exec format error
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "pysam/libcalignmentfile.pyx", line 742, in pysam.libcalignmentfile.AlignmentFile.__cinit__
    File "pysam/libcalignmentfile.pyx", line 941, in pysam.libcalignmentfile.AlignmentFile._open
  OSError: [Errno 8] could not open alignment file `mybam.bam`: Exec format error
  
  ```
  
  **Tags:** python, phylogenetics, sam, file-formats, pysam
  
  > **Comments:**
  > **username_1**: this is something related to the file not with python. Check the file with the linux command `file mybam.bam` just to know that you have a bam instead of a sam, text format. If you have a compressed file in bam format, check the head with `samtools view mybam.bam | head -100`, if this fails check the mapping software again where you got the alignment from.  
  > **username_2**: https://www.biostars.org/p/284417/  
  > **username_0**: Thank you for the help. samtools view mybam.bam | head -100 outputed a hundred lines, which have no index. As there is no index, I was able to retrieve the content with: for read in samfile.fetch(until_eof=True):     print(read)  
  
  ---
  
  ## 0 answers
  
  