该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
请教一下高手。我建立的临时文件删不掉,请问该怎么办?
PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmpgj8gb6vx'
使用了os.close(config_file) 关闭这个临时文件时提示
TypeError: an integer is required (got type str)
代码如下:
def save_config_to_file(config_content):
'''将配置信息也写到日志临时文件中'''
config_file = tempfile.mkstemp(text=True)[1] #创建一个临时文件,返回一个元组,包括安全级别和路径
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):
'''把结果上传到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()
主函数下:
os.close((config_file))
os.remove(config_file) #删除本地保存的配置文件