Python GUI无法看到全局变量

时间:2014-06-29 12:26:19

标签: python variables python-2.7 python-3.x global-variables

我正在运行一个函数。一旦运行,我希望能够重新使用驻留在函数模块中的target_dir变量(由另一个函数使用),因此我将变量设置为global

然而,当我在Python GUI中键入变量的名称:target_dir时,我收到此消息:

NameError: name 'target_dir' is not defined

这是模块:

def SECdownload(year, month):
    import os
    from urllib.request import urlopen
    root = None
    feedFile = None
    feedData = None
    good_read = False
    itemIndex = 0
    edgarFilingsFeed = 'http://www.sec.gov/Archives/edgar/monthly/xbrlrss-' + str(year) + '-' + str(month).zfill(2) + '.xml'
    print(edgarFilingsFeed)
    #print( edgarFilingsFeed ) #from the slides
    if not os.path.exists( "sec/" + str(year) ):
        os.makedirs( "sec/" + str(year) )
    if not os.path.exists( "sec/" + str(year) + '/' + str(month).zfill(2) ):
        os.makedirs( "sec/" + str(year) + '/' + str(month).zfill(2) )
    global target_dir
    target_dir = "sec/" + str(year) + '/' + str(month).zfill(2) + '/'
    try:
        feedFile = urlopen( edgarFilingsFeed ) # urlopen will not work (python 3) needs from urllib.request import urlopen
        try:
            feedData = feedFile.read()
            good_read = True
        finally:
            feedFile.close()
    except HTTPError as e:
        print( "HTTP Error:", e.code )

P.S。我也试过这个想法:

 if not os.path.exists( "sec/" + str(year) + '/' + str(month).zfill(2) ):
        os.makedirs( "sec/" + str(year) + '/' + str(month).zfill(2) )
global target_dir
    target_dir = "sec/" + str(year) + '/' + str(month).zfill(2) + '/'

有关如何在运行target_dir函数后使变量SECdownload可用的任何想法?

3 个答案:

答案 0 :(得分:1)

我要尝试的第一件事是将target_dir初始化为一个默认值,超出函数范围。那就是:

target_dir = None

target_dir = /some/directory
def SECdownload(year, month):
...

答案 1 :(得分:1)

import os
from urllib.request import urlopen

global target_dir

def SECdownload(year, month):

root = None
feedFile = None
feedData = None
good_read = False
itemIndex = 0
edgarFilingsFeed = 'http://www.sec.gov/Archives/edgar/monthly/xbrlrss-' + str(year) + '-' + str(month).zfill(2) + '.xml'
print(edgarFilingsFeed)
#print( edgarFilingsFeed ) #from the slides
if not os.path.exists( "sec/" + str(year) ):
    os.makedirs( "sec/" + str(year) )
if not os.path.exists( "sec/" + str(year) + '/' + str(month).zfill(2) ):
    os.makedirs( "sec/" + str(year) + '/' + str(month).zfill(2) )


target_dir = "sec/" + str(year) + '/' + str(month).zfill(2) + '/'
try:
    feedFile = urlopen( edgarFilingsFeed ) # urlopen will not work (python 3) needs from urllib.request import urlopen
    try:
        feedData = feedFile.read()
        good_read = True
    finally:
        feedFile.close()
except HTTPError as e:
    print( "HTTP Error:", e.code )

答案 2 :(得分:1)

你可以退货。只需在功能结束时添加

def SECdownload(year, month):
    import os
    from urllib.request import urlopen
    root = None
    feedFile = None
    feedData = None
    good_read = False
    itemIndex = 0
    edgarFilingsFeed = 'http://www.sec.gov/Archives/edgar/monthly/xbrlrss-' + str(year) + '-' + str(month).zfill(2) + '.xml'
    print(edgarFilingsFeed)
    #print( edgarFilingsFeed ) #from the slides
    if not os.path.exists( "sec/" + str(year) ):
        os.makedirs( "sec/" + str(year) )
    if not os.path.exists( "sec/" + str(year) + '/' + str(month).zfill(2) ):
        os.makedirs( "sec/" + str(year) + '/' + str(month).zfill(2) )
    target_dir = "sec/" + str(year) + '/' + str(month).zfill(2) + '/'
    try:
        feedFile = urlopen( edgarFilingsFeed ) # urlopen will not work (python 3) needs from urllib.request import urlopen
        try:
            feedData = feedFile.read()
            good_read = True
        finally:
            feedFile.close()
    except HTTPError as e:
        print( "HTTP Error:", e.code )
    return target_dir

然后,当你可以的时候,它会返回target_dir。

target_dir = SECdownload(someYear, someMonth)

如果您想使用全局,则需要在之前初始化变量。

target_dir = None
def SECdownload(year, month):
    import os
    from urllib.request import urlopen
    root = None
    feedFile = None
    feedData = None
    good_read = False
    itemIndex = 0
    edgarFilingsFeed = 'http://www.sec.gov/Archives/edgar/monthly/xbrlrss-' + str(year) + '-' + str(month).zfill(2) + '.xml'
    print(edgarFilingsFeed)
    #print( edgarFilingsFeed ) #from the slides
    if not os.path.exists( "sec/" + str(year) ):
        os.makedirs( "sec/" + str(year) )
    if not os.path.exists( "sec/" + str(year) + '/' + str(month).zfill(2) ):
        os.makedirs( "sec/" + str(year) + '/' + str(month).zfill(2) )
    global target_dir
    target_dir = "sec/" + str(year) + '/' + str(month).zfill(2) + '/'
    try:
        feedFile = urlopen( edgarFilingsFeed ) # urlopen will not work (python 3) needs from urllib.request import urlopen
        try:
            feedData = feedFile.read()
            good_read = True
        finally:
            feedFile.close()
    except HTTPError as e:
        print( "HTTP Error:", e.code )

你可以像那样导入它。

import test #name of your file

test.SECdownload(2014, 5)
print(test.target_dir)