2017-09-16 64 views
0

在Python中,我有一個腳本的這一部分:和我試圖插入兩個變量的時間和日期,但我得到一個錯誤

ip = ['10.25.128.225', '10.25.128.223', '10.25.128.224', '10.25.128.241'] 
time = time.strftime("%H:%M:%S") 
date = time.strftime("%d/%m/%Y") 

for address in ip: 
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    result = sock.connect_ex((address, 9999)) 
    if result != 0: 
     print('There is an issue with the {} address at {} on {} '.format(address, time, date)) 

每當我運行的代碼,但是,我得到錯誤:

date = time.strftime("%d/%m/%Y") 
AttributeError: 'str' object has no attribute 'strftime' 

我想我必須建立一個數據類型,但我仍然是一種新的python /編程。有什麼想法嗎?謝謝!

+0

命名你的時間變量不同 - 它陰影'時間'模塊 –

回答

1

time模塊被覆蓋time = time.strftime("%H:%M:%S"),它返回一個字符串對象。您可以添加尾部下劃線:time_ = time.strftime("%H:%M:%S")或更改名稱以避免衝突。

1

你犯了一個錯誤:time = time.strftime("%H:%M:%S")

時間現在是一個字符串,沒有更多的time對象,您用於生產的時間字符串。你必須重命名左邊time

相關問題