python - Passing a file as a function argument -



python - Passing a file as a function argument -

if have text file (sequencedata.txt) in directory named documents:

/~/documents/sequencedata.txt

how define function named sequenceinfo count number of entries start # in file of above pathway , homecoming count syntax "there * entries", in * counted number of entries?

what have far:

#!/usr/bin/python def sequenceinfo(sequencedata): my_file = open("/~/documents/sequencedata.txt") my_dna = my_file.read() ecoliseq = my_dna.count('#') homecoming ecoliseq print("there " + str(ecoliseq) + " sequences")

there few things here:

the name ecoliseq not visible outside of function sequenceinfo. consequentially, print line raise nameerror because python unable find ecoliseq. however, ecoliseq returned function, means can access value calling function , printing result.

you made sequenceinfo take argument never used. supposed path file? i'm going assume is, should remove if not.

not major problem, not closing file when done it. should manage , release resources programme aquires. easiest way files utilize with-statement open them, automatically close them you.

in all, code should following:

#!/usr/bin/python def sequenceinfo(path): open(path) my_file: my_dna = my_file.read() ecoliseq = my_dna.count('#') homecoming ecoliseq print("there " + str(sequenceinfo("/~/documents/sequencedata.txt")) + " sequences")

also, there may 4th problem in my_dna.count('#') homecoming count of every # in file. said in question want count entries start #. need prepare bug if so.

python file function

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -