Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';我的Python怎么了?_Python_Syntax_Syntax Error - Fatal编程技术网

什么';我的Python怎么了?

什么';我的Python怎么了?,python,syntax,syntax-error,Python,Syntax,Syntax Error,我编写了这个脚本来帮助我在特定文件中执行搜索。它的功能类似于grep,但我需要比grep提供的功能更具体一点的东西,所以我正在尝试Python 我有一个类有三种方法。第二种方法listOccurrencesOfToDoInFile(第30行)失败。这是一个很小的脚本,但由于制表符在Python中很重要,所以我将其放入了一个脚本中。有人能告诉我为什么我的看似有效的Python是无效的吗 import os import keyword import sys class Toodles:

我编写了这个脚本来帮助我在特定文件中执行搜索。它的功能类似于grep,但我需要比grep提供的功能更具体一点的东西,所以我正在尝试Python

我有一个类有三种方法。第二种方法
listOccurrencesOfToDoInFile
(第30行)失败。这是一个很小的脚本,但由于制表符在Python中很重要,所以我将其放入了一个脚本中。有人能告诉我为什么我的看似有效的Python是无效的吗

import os
import keyword
import sys


class Toodles:

    def walkAndList(directory):
        for dirname, dirnames, filenames in os.walk(directory):

            # print path to all filenames.
            for filename in filenames:
                workingFilename = os.path.join(dirname, filename)
                if(isSourceFile(filename)):
                    listOccurrencesOfToDoInFile(filename)

            # Advanced usage:
            # editing the 'dirnames' list will stop os.walk() from recursing into there.
            if '.git' in dirnames:
                # don't go into any .git directories.
                dirnames.remove('.git')

            for dirs in dirnames:
                self.walkAndList(os.path.join(dirname, dirs)


    #   Find occurences of "todo" and "fixme" in a file
    #   If we find such an occurence, print the filename, 
    #   the line number, and the line itself.
    def listOccurrencesOfToDoInFile(aFileName):
        input = open(aFileName)
        currentLine = 1
        for (line in input):
            line = line.lower()
            currentLine = currentLine + 1
            needle = "todo"
            if (needle in line):
                sys.stdout.write(aFileName + " (" + str(currentLine) + ")" + line)

    #Todo: add a comment
    def isSourceFile(self, name):
        fileName, fileExtension = os.path.splitext(name)
        if (".m" in fileExtension or ".c" in fileExtension or ".h" in fileExtension):
            return True
        return False


if ( __name__ == "__main__") {
    a = Toodles()
    a.walkAndList('.')
}   

您错过了一个右括号:

 self.walkAndList(os.path.join(dirname, dirs)
for line in input:
if self.isSourceFile(filename):
    self.listOccurrencesOfToDoInFile(filename)
请注意,有两个开口,但只有一个开口

下一个问题是,您使用的是更向下的大括号:

if ( __name__ == "__main__") {
    a = Toodles()
    a.walkAndList('.')
}
这是Python,不是C、Java或Javascript;拆下大括号并使用冒号:

if __name__ == "__main__":
    a = Toodles()
    a.walkAndList('.')
然后,在
for
语句中使用括号是不合法的:

for (line in input):
删除这些括号:

 self.walkAndList(os.path.join(dirname, dirs)
for line in input:
if self.isSourceFile(filename):
    self.listOccurrencesOfToDoInFile(filename)
下一个问题是,您没有为以下两种方法定义
self

def walkAndList(directory):

添加
self
作为第一个参数:

def walkAndList(self, directory):
# ...
def listOccurrencesOfToDoInFile(self, aFileName):
接下来,如果要将
Toodles.isSourceFile()
Toodles.ListOccurrenceSoftodoindle()
方法视为全局方法,则需要在这些方法前面添加
self.
,以便在当前实例上将它们作为方法调用:

if(isSourceFile(filename)):
    listOccurrencesOfToDoInFile(filename)
应为(无多余括号):

然后,您可以在希望使用
workingFilename
的位置(包括路径)引用
filename
(缺少路径):

否则打开这些文件时会出错

然后,您的文件扩展名测试有缺陷;只需使用
.endswith()
即可防止匹配文件,如
.csh
.h
。此外,不需要先询问
if
来测试某个内容是否为
True
,然后分别返回
True
False
;您可以直接返回布尔测试:

在序列上循环计数时,使用
enumerate()
函数为您生成计数器:

for currentLine, line in enumerate(input, 1):
    line = line.lower()
从1开始计算行数

请注意,
os.walk()
已经为您遍历了子目录。无需再次递归。通过删除以下行来删除递归:

for dirs in dirnames:
    self.walkAndList(os.path.join(dirname, dirs))
经过一些改进(使用
With
再次关闭打开的文件,将
指针设置在循环外部,早期过滤,使用字符串格式,写出原始行,而不是小写),完整脚本变为:

import os
import sys


class Toodles(object):
    def walkAndList(self, directory):
        for dirname, dirnames, filenames in os.walk(directory):
            for filename in filenames:
                if self.isSourceFile(filename):
                    workingFilename = os.path.join(dirname, filename)
                    self.listOccurrencesOfToDoInFile(workingFilename)

            # Advanced usage:
            # editing the 'dirnames' list will stop os.walk() from recursing into there.
            if '.git' in dirnames:
                # don't go into any .git directories.
                dirnames.remove('.git')

    #   Find occurences of "todo" and "fixme" in a file
    #   If we find such an occurence, print the filename,
    #   the line number, and the line itself.
    def listOccurrencesOfToDoInFile(self, aFileName):
        needle = "todo"
        with open(aFileName) as input:
            for currentLine, line in enumerate(input, 1):
                if needle in line.lower():
                    sys.stdout.write('{}: ({}){}'.format(aFileName, currentLine, line))

    #Todo: add a comment
    def isSourceFile(self, name):
        return name.endswith(('.m', '.c', '.h'))


if __name__ == "__main__":
    a = Toodles()
    a.walkAndList('.')

您错过了一个右括号:

 self.walkAndList(os.path.join(dirname, dirs)
for line in input:
if self.isSourceFile(filename):
    self.listOccurrencesOfToDoInFile(filename)
请注意,有两个开口,但只有一个开口

下一个问题是,您使用的是更向下的大括号:

if ( __name__ == "__main__") {
    a = Toodles()
    a.walkAndList('.')
}
这是Python,不是C、Java或Javascript;拆下大括号并使用冒号:

if __name__ == "__main__":
    a = Toodles()
    a.walkAndList('.')
然后,在
for
语句中使用括号是不合法的:

for (line in input):
删除这些括号:

 self.walkAndList(os.path.join(dirname, dirs)
for line in input:
if self.isSourceFile(filename):
    self.listOccurrencesOfToDoInFile(filename)
下一个问题是,您没有为以下两种方法定义
self

def walkAndList(directory):

添加
self
作为第一个参数:

def walkAndList(self, directory):
# ...
def listOccurrencesOfToDoInFile(self, aFileName):
接下来,如果要将
Toodles.isSourceFile()
Toodles.ListOccurrenceSoftodoindle()
方法视为全局方法,则需要在这些方法前面添加
self.
,以便在当前实例上将它们作为方法调用:

if(isSourceFile(filename)):
    listOccurrencesOfToDoInFile(filename)
应为(无多余括号):

然后,您可以在希望使用
workingFilename
的位置(包括路径)引用
filename
(缺少路径):

否则打开这些文件时会出错

然后,您的文件扩展名测试有缺陷;只需使用
.endswith()
即可防止匹配文件,如
.csh
.h
。此外,不需要先询问
if
来测试某个内容是否为
True
,然后分别返回
True
False
;您可以直接返回布尔测试:

在序列上循环计数时,使用
enumerate()
函数为您生成计数器:

for currentLine, line in enumerate(input, 1):
    line = line.lower()
从1开始计算行数

请注意,
os.walk()
已经为您遍历了子目录。无需再次递归。通过删除以下行来删除递归:

for dirs in dirnames:
    self.walkAndList(os.path.join(dirname, dirs))
经过一些改进(使用
With
再次关闭打开的文件,将
指针设置在循环外部,早期过滤,使用字符串格式,写出原始行,而不是小写),完整脚本变为:

import os
import sys


class Toodles(object):
    def walkAndList(self, directory):
        for dirname, dirnames, filenames in os.walk(directory):
            for filename in filenames:
                if self.isSourceFile(filename):
                    workingFilename = os.path.join(dirname, filename)
                    self.listOccurrencesOfToDoInFile(workingFilename)

            # Advanced usage:
            # editing the 'dirnames' list will stop os.walk() from recursing into there.
            if '.git' in dirnames:
                # don't go into any .git directories.
                dirnames.remove('.git')

    #   Find occurences of "todo" and "fixme" in a file
    #   If we find such an occurence, print the filename,
    #   the line number, and the line itself.
    def listOccurrencesOfToDoInFile(self, aFileName):
        needle = "todo"
        with open(aFileName) as input:
            for currentLine, line in enumerate(input, 1):
                if needle in line.lower():
                    sys.stdout.write('{}: ({}){}'.format(aFileName, currentLine, line))

    #Todo: add a comment
    def isSourceFile(self, name):
        return name.endswith(('.m', '.c', '.h'))


if __name__ == "__main__":
    a = Toodles()
    a.walkAndList('.')

这是一个有趣的
\uuuuuuuuuuuuuuu主块。其他人已经指出了这个问题。我想补充一点:(1)你为什么使用
sys.stout.write
而不只是
print
?(2)
类Toodles
有什么用途?什么是图德尔?您不需要类来分组相关功能,您的模块已经达到了这个目的。如果您不能解释什么是Toodles以及为什么这些函数是Toodles对象的方法,那么最好将它们作为直接函数实现。(3) 有人已经说过了,但是
if(condition):
中的括号(同样在
for
循环中)是多余的。感谢您的反馈。正如我所说,我对Python完全陌生,因此这是一次学习经历。这是一个有趣的
\uuuu main\uuuu
块。其他人已经指出了这个问题。我想补充一点:(1)你为什么使用
sys.stout.write
而不只是
print
?(2)
类Toodles
有什么用途?什么是图德尔?您不需要类来分组相关功能,您的模块已经达到了这个目的。如果您不能解释什么是Toodles以及为什么这些函数是Toodles对象的方法,那么最好将它们作为直接函数实现。(3) 有人已经说过了,但是
if(co)中的括号