-1

Hello I am pretty new to python and I want to do the following:

I have a function that opens a file, reads the file, closes the file and returns the data:

def getFastaFromFile(filename):
    """ Read a fasta file (filename) from disk and return
    its full contents as a string"""
    inf=open(filename)
    data=inf.read()
    inf.close()
    return data

The data that is being returned are a few lines with strings.

What I want to do is have another function that uses the data from the first function and perform the .readlines(), .readline() and .count() commands

My second function:

def printTableFromFasta(fastarec):
    a= data.readlines()
    for i in range(a)
        b= data.readline()
        c= b.count('A')
        print(c)

As output I would like to print the amount of times string "A" appears for every line from the data. The problem I get with this code is that the data doesn't get recognized.

2 Answers 2

1

First, you need to pass the data you are wanting to read into the second function, like so

def printTableFromFasta(data):

In order to get this from your first function, try returning the entire contents of the file

def getFastaFromFile(filename):
    with open(filename, 'r') as inf: # handles open and close
        data = inf.readlines()       # Returns the entire file as a list of strings
    return data

Your function call will look something like this

printTableFromFasta(getFastaFromFile(filename))

Then, in your second function, you don't need to call readlines, it's already a list.

def printTableFromFasta(data):
    for line in data            # look at each line
        print(line.count('A'))  # count 'A'

Edit: To only read from the second function and not touch the first function

def printTableFromFasta(filename):
    with open(filename, 'r') as inf: # handles open and close
        for line in inf.readlines()  # look at each line
            print(line.count('A'))   # count 'A'
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Groger, this works, but is there a way to do this without changing the first function? I only want to do the reading commands in the second function.
@NawinNarain The file closes when getFastaFromFile ends. If you only want to change the second function, I can make an edit, but it involves only using the second function.
Thank you for the edit Groger! This is what I needed!
0

Remember that the data variable in the first function is local. It cannot be accessed from outside the function it is defined in.

For example, the getName() function returns a variable which is locally called data but you access the value by calling the function.

def getName(user_id):
    data = "Your name is " + str(user_id)
    return data 

# Throws an error, because data in undefined 
name = getName("Bobby")
print(data)

# Working code, prints "Your name is Bobby"
name = getName("Bobby")
print(name)

There are no rules against calling one function from inside another. Instead of a = data.readlines() try a = getFastaFromFile("dna.fasta') as well as changing data = inf.read() to data = inf.readlines()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.