代码之家  ›  专栏  ›  技术社区  ›  Chris Macaluso

当变量存在时,为什么会出现Flake8 F821错误?

  •  0
  • Chris Macaluso  · 技术社区  · 7 年前

    main

    我试着把它添加为 global var ,并放置 tox.ini ignore = F821 但这也没有注册。A.

    有什么建议吗?下面的代码块仅供参考。 new_folder

    def createDestination(self):
        '''
        split the src variable for machine type
        and create a folder with 'Evo' - machine
        '''
        s = src.split('\\')
        new_folder = (dst + '\\Evo ' + s[-1])
        if not os.path.exists(new_folder):
            os.makedirs(new_folder)
            return self.new_folder
    
    
    def copyPrograms(new_folder):
        '''
        find all TB-Deco programs in second tier directory.
        '''
        # create file of folders in directory
        folder_list = os.listdir(src)
        # iterate the folder list
        for folder in folder_list:
            # create a new directory inside each folder
            folder_src = (src + '\\' + folder)
            # create a list of the files in the folder
            file_list = os.listdir(folder_src)
            # iterate the list of files
            for file in file_list:
                # if the file ends in .part .PART .dbp or .DBP - add it to a list
                if (file.endswith('.part') or file.endswith('.PART') or
                        file.endswith('.dbp') or file.endswith('.DBP')):
                    # create a location variable for that file
                    file_src = (src + folder + '\\' + file)
                    # copy the file from the server to dst folder
                    new_file = ('Evo ' + file)
                    file_dst = (new_folder + '\\' + new_file)
                    if not os.path.exists(file_dst):
                        shutil.copy2(file_src, file_dst)
    
    
    def main():
        createDestination()
        copyPrograms(new_folder)
    
    
    if __name__ == "__main__":
        main()
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   chepner    7 年前

    第一个问题是 createDestination 从不定义属性 self.new_folder new_folder . 缩进也将关闭,因为您希望返回缩进 新文件夹

    def createDestination(self):
        '''
        split the src variable for machine type
        and create a folder with 'Evo' - machine
        '''
        s = src.split('\\')
        new_folder = (dst + '\\Evo ' + s[-1])
        if not os.path.exists(new_folder):
            os.makedirs(new_folder)
        return new_folder  # not self.new_folder
    

    其次,您从未指定 创建目的地 copyPrograms 作为论据。

    def main():
        new_folder = createDestination()
        copyPrograms(new_folder)
    

    在…内 创建目的地 与中同名的不同 main 主要的

    def main():
        d = createDestination()
        copyPrograms(d)
    

    你甚至都不知道 需要

    def main():
        copyPrograms(createDestination())