Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help needed!
#1
Hi, I'm trying to make a loop for some files. I want to use a number of rows in the file to split it a half... and I'm stuck please help (python 2 :))

import os
import sys

for line in open("samples.txt"):
    if line.startswith("#"): continue
    line = line.rstrip("\r\n").split()
    sample = line[0]
    library_type = line[1]
 
    os.system("wc -l < bam_split/%s/%s_mapped.sam >bam_split/%s/%s_reads.txt" % (sample, sample, sample, sample))

    with open("bam_split/%s/%s_reads.txt" % (sample, sample)) as factor:
        val = factor.readlines()
        val = int(val)        
        val2 = val / 2
        val3 = val2 + 1


        os.system("sed -n 1,%sp bam_split/%s/%s_noheader_mapped.sam > bam_split/%s/%s_noheader_mapped_1.sam " % (val2, sample, sample, sample, sample))
        os.system("sed -n %s,%sp bam_split/%s/%s_mapped.sam > bam_split/%s/%s_noheader_mapped_2.sam" % (val3, val, sample, sample, sample, sample))

Error:
TypeError: int() argument must be a string or a number, not 'list'
Reply
#2
The problem seems to be in the line #14. val is a list, a list of lines that were read from the file.
You probably need an extra loop, e.g.
for val in factor.readlines(): # alternative:  for val in factor:
   val = int(val)
   ...
Reply
#3
Thank you! it works :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020