1

I am trying to call some functions from one file in another file on python, but whenever I do that I keep getting an error. This is the code I have for the first file called myfile.py

def readfile('C:\Users\kprab\Documents\python\data.asc'):
    # to read in the data from a file
    import pylab
    from numpy import genfromtxt
    # get data
    data = genfromtxt('C:\Users\kprab\Documents\python\data.asc', delimiter=',')
    print data

def proj(data):
    # to create an image from data
    import numpy
    from matplotlib import pyplot as plt
    x = data
    plt.imshow(x, cmap='gray', interpolation='nearest', vmin=0, vmax=255)
    plt.savefig('text.png')
    plt.show()

and then I am trying to call these function in another file but with different data. This codes looks like the following:

import myfile

a = myfile.readfile('C:\Users\kprab\Documents\python\HPOPUP2_201507071256_1')
print a
b = myfile.proj(a)
print b

and the error I get is

def readfile('C:\Users\kprab\Documents\python\data.asc'):
                                                      ^    
SyntaxError: invalid syntax

Any help is appreciated. Thanks!

3
  • reformat the code in your question Commented Jun 28, 2017 at 14:58
  • 1
    I think it's because you need to have an identifier in the signature and not an actual argument. Try def readfile(fpath): and before that mypath='C...' and then call readfile(mypath) Commented Jun 28, 2017 at 15:00
  • 1
    def readfile('C:\Users\kprab\Documents\python\data.asc'): doesn't make any sense. You need to give a name to use for the parameter, not the string you intend to pass it later. Commented Jun 28, 2017 at 15:00

3 Answers 3

2

You messed the function definition.

def readfile('C:\Users\kprab\Documents\python\data.asc'):
    # to read in the data from a file
    import pylab
    from numpy import genfromtxt
    # get data
    data = genfromtxt('C:\Users\kprab\Documents\python\data.asc', delimiter=',')

Must be something like

def readfile(filename='C:\Users\kprab\Documents\python\data.asc'):
    import pylab
    from numpy import genfromtext
    data = genfromtxt(filename)

Note that I defined a function parameter filename with default value 'C:\Users\kprab\Documents\python\data.asc' and I use this filename later in function.

Sign up to request clarification or add additional context in comments.

3 Comments

Okay I understand what you are saying, if I define it this way when I go to call this function in the other file with other data and I define the filepath as a = myfile.readfile(filepath = 'C:\Users\kprab\Documents\python\HPOPUP2_201507071256_1.asc') b = myfile.proj(a) - I get the error that filepath is not defined.
No, you are confusing the syntax used to define and the one to call, when you call your function, do myfile.readfile('path'), don't add the parameter name at this moment
When I go to call it and even if I just put the actual path of the new data, I still get the same error.
1

Could you please clarify something: in your myfile, are you defining your function as:

def readfile('C:\Users\kprab\Documents\python\data.asc') ?

Because if so, that is why you are having the syntax error. You are creating a function that are passing a value already given, rather than a parameter which could be used by your function. Try: import pylab from numpy import genfromtxt def readfile(file_path='C:\Users\kprab\Documents\python\data.asc'): data = genfromtxt(file_path, delimiter=',') print data return data

Then you can give in your actual code whatever filepath you want.

4 Comments

Okay I understand what you are saying, if I define it this way when I go to call this function in the other file with other data and I define the filepath as a = myfile.readfile(filepath = 'C:\Users\kprab\Documents\python\HPOPUP2_201507071256_1.asc'‌​) b = myfile.proj(a) - I get the error that filepath is not defined.
Does the C:\Users\kprab\Documents\python\HPOPUP2_201507071256_1.asc exist?
Yes it does, I tested it out by putting it in the actual function and it works fine. Now I'm just calling it with the actual file path and not defining anything as the filepath. The error is that the filepath is not defined and it's from the original myfile.py file
Please provide an edit to your new code. I do not understand what is your issue at this point. The way I see it running, it does in my case.
1

your argument to readfile() must have a name.

def readfile(foo='C:\Users\kprab\Documents\python\data.asc'):
    bla bla

some explanation: based on your comments i think you need a little explation about functions. functions are stored procedures that you or anyone can repeatedly use.

An example of function is the savefig() function you used to save the image in your program. As you can see the creator of this function doesn't know you nor your filename, she doesn't even know what you are going to do with it. this concept is called abstraction The only thing she knows is that a program that wants to save the image will give an image object and a file name. Given these two arguments the function will do what it is supposed to, save!

So in your case your function expects a file name, you do that with a variable representing that file name - so that it will work dynamically, lets call it file_name. so anyone wanting to use your function will call it with a file name, for your finction it is just a file name, so inside your function anytime you want to manipulate or make use of the passed file name you say file_name.

3 Comments

Yes I am still learning how functions work. So for the readfile function I changed it so the variable is "filepath" and that's the variable that is in the rest of the function. This way whenever I want to call this function, I just give it an actual file path. Is that correct? If so when I call this function in my other file with an actual file path, I get the error that filepath is not defined.
exactly, but without quotes like def readfile(filepath) because filepath is a variable, not a string.
Okay the first function is working. The second function is not working, the error I get is: Image data can not convert to float. For some reason it's not saving the the data array which has to be used for the second function. Anyways thanks for your help!

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.