我遇到了一個問題,在Python中寫入NamedTemporaryFile然後再讀回。該函數通過tftpy將文件下載到臨時文件,讀取它,散列內容,然後將散列摘要與原始文件進行比較。有問題的函數如下所示:Python NamedTemporaryFile - ValueError閱讀時
def verify_upload(self, image, destination):
# create a tftp client
client = TftpClient(ip, 69, localip=self.binding_ip)
# generate a temp file to hold the download info
if not os.path.exists("temp"):
os.makedirs("temp")
with NamedTemporaryFile(dir="temp") as tempfile, open(image, 'r') as original:
try:
# attempt to download the target image
client.download(destination, tempfile, timeout=self.download_timeout)
except TftpTimeout:
raise RuntimeError("Could not download {0} from {1} for verification".format(destination, self.target_ip))
# hash the original file and the downloaded version
original_digest = hashlib.sha256(original.read()).hexdigest()
uploaded_digest = hashlib.sha256(tempfile.read()).hexdigest()
if self.verbose:
print "Original SHA-256: {0}\nUploaded SHA-256: {1}".format(original_digest, uploaded_digest)
# return the hash comparison
return original_digest == uploaded_digest
的問題是,每次我試圖用ValueError - I/O Operation on a closed file
執行線uploaded_digest = hashlib.sha256(tempfile.read()).hexdigest()
的應用程序錯誤出時間。由於with
塊沒有完成,我正在努力理解爲什麼臨時文件將被關閉。我能想到的唯一可能是tftpy在下載完成後關閉了文件,但我無法在tftpy源文件中找到任何會發生這種情況的點。請注意,我也嘗試插入行tempfile.seek(0)
以便將文件恢復爲正確的閱讀狀態,但這也給我ValueError
。
tftpy是否可能關閉文件?我讀過在NamedTemporaryFile中可能導致此問題的錯誤?爲什麼文件在with
塊定義的參考超出範圍之前關閉?
唉!當然你是對的。發生這種情況後,我可以重新打開臨時文件嗎? – Kin3TiX
@ Kin3TiX:正常情況下,只要文件關閉,文件就會被刪除,但是您可以將'delete = False'傳遞給'NamedTemporaryFile'構造函數來防止這種情況發生,然後通過名稱重新打開文件。如果你這樣做,請記住,當你完成它時,你將負責刪除文件。 – user2357112