简体   繁体   English

如何在Python中的另一个文件中调用函数?

[英]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. 我试图在python的另一个文件中的一个文件中调用某些函数,但是每当执行该操作时,我都会不断收到错误消息。 This is the code I have for the first file called myfile.py 这是我第一个文件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. 请注意,我使用默认值'C:\\Users\\kprab\\Documents\\python\\data.asc'定义了一个函数参数filename ,并且稍后在函数中使用此filename

your argument to readfile() must have a name. 您对readfile()的参数必须有一个名称。

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. 函数的一个示例是用于将图像保存到程序中的savefig()函数。 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. 因此,在您的情况下,您的函数需要一个文件名,您可以使用一个代表该文件名的变量来实现该目的-为了使其能够动态工作,请将其命名为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 . 因此,任何想要使用您的函数的人都将使用文件名来调用它,对于您的功能来说,它仅仅是一个文件名,因此, 在函数中任何您想要操纵或使用传递的文件名的地方,都可以说file_name

Could you please clarify something: in your myfile , are you defining your function as: 您能否澄清一下:在myfile中 ,您是否将函数定义为:

def readfile('C:\\Users\\kprab\\Documents\\python\\data.asc') ? 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 尝试: 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. 然后,您可以在实际代码中输入所需的任何文件路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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