Read a text file and save value as variable in python

This question already has an answer here:

  • Using global variables in a function other than the one that created them 18 answers

  • 如果您只想与其他功能共享,请使用全局功能

    id = 0
    def main():
        global id
        ...
    

    id = 0 # declare it in the global scope
    
    def main():
        # using the global keyword will let you access it in the function scope
        global id 
        input = file(infile, 'r')
        lines = input.readlines()
        input.close()
        i = 0
        for line in lines:
            i += 1
            id = int(line)
            WorkOnIDThread(id)
    main()
    print id
    
    链接地址: http://www.djcxy.com/p/23886.html

    上一篇: Python加密函数和全局变量

    下一篇: 读取一个文本文件并将值作为变量保存在python中