Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SIGKILL adding column in a simple python script

I have a file that has a oneline header and a long column with values. I want to add a second column with values since 10981 (step = 1) until the end of the file (ommiting the header, of course). The problem is that the script needs a lot of memory and my pc crashes, probably due to the script is not well made (sorry, I am new programming!). The script that I have done is this:

with open ('chr1.phyloP46way.placental2.wigFix', 'w') as file_open:
    num = 10981
    text = file_open.readlines()
    next (text)
    for line in text:
        num = num + 1
        print line.strip() + '\t' + str(num)

As my PC crashes when I run it, I tried to test it in pycharm with the following error, what I have seen is probably due to lack of memory:

Process finished with exit code 137 (interrupted by signal 9: SIGKILL)

Any idea to solve this?

Thank you very much!

like image 937
JaimeG Avatar asked Feb 25 '26 19:02

JaimeG


1 Answers

If your system is running out of resources, the likely culprit is the readlines() call, which causes Python to try to load the entire file into memory. There's no need to do this... a file object can itself be used as an iterator to read the file line by line:

with open ('chr1.phyloP46way.placental2.wigFix', 'w') as file_open:
    num = 10981
    next (file_open)
    for line in file_open:
        num = num + 1
        print line.strip() + '\t' + str(num)
like image 188
glibdud Avatar answered Feb 27 '26 09:02

glibdud