简体   繁体   中英

How to call a function in another file in Python?

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!

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM