python删除文件一行数据、不使用临时文件_python下tempfile 临时文件删除问题
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
請教一下高手。我建立的臨時文件刪不掉,請問該怎么辦?
PermissionError: [WinError 32] 另一個程序正在使用此文件,進(jìn)程無法訪問。: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmpgj8gb6vx'
使用了os.close(config_file) 關(guān)閉這個臨時文件時提示
TypeError: an integer is required (got type str)
代碼如下:
def save_config_to_file(config_content):
'''將配置信息也寫到日志臨時文件中'''
config_file = tempfile.mkstemp(text=True)[1] #創(chuàng)建一個臨時文件,返回一個元組,包括安全級別和路徑
with codecs.open(config_file, "w", "utf-8") as f:
f.write(config_content.decode("utf-8") + "\n")
logging.debug("tempfile - " + config_file) #將配置信息也寫到日志臨時文件中
return config_file
def put_config_file_to_ftp(config_dir, config_file, ftp_ip, ftp_username, ftp_password):
'''把結(jié)果上傳到FTP'''
client = ftplib.FTP()
client.connect(ftp_ip)
client.login(ftp_username, ftp_password)
try:
server_dir = client.mkd(config_dir) #建一個目錄
except ftplib.error_perm as e:
server_dir = config_dir
logging.debug(traceback.format_exc())
server_file = date.today().strftime("%Y-%m-%d") + "-config.txt"
with open(config_file, "rb") as f:
client.storbinary("STOR " + os.path.join(server_dir, server_file), f)
client.quit()
client.close()
主函數(shù)下:
os.close((config_file))
os.remove(config_file) #刪除本地保存的配置文件
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的python删除文件一行数据、不使用临时文件_python下tempfile 临时文件删除问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mplus 软件_Mplus 7.4 软
- 下一篇: vb 变量赋值为当前选定单元格_第7篇: