gpt4 book ai didi

Python GUI 无法看到全局变量

转载 作者:行者123 更新时间:2023-11-30 23:22:27 25 4
gpt4 key购买 nike

我正在运行一个函数。一旦运行,我希望能够重新使用 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 )

附注我也尝试过这种识别:

 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 后可用功能?

最佳答案

你可以返回。只需在函数末尾添加即可

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)

关于Python GUI 无法看到全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24476058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com