pythontemp_python下tempfile 临时文件删除问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

请教一下高手。我建立的临时文件删不掉,请问该怎么办?

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) #删除本地保存的配置文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值