简体   繁体   English

python脚本执行后清理/ tmp目录

[英]/tmp directory clean up after python script execution

I am using python and invoking external utility "send-data" in my .py file as shown below, 我正在使用python并在.py文件中调用外部实用程序“ send-data”,如下所示,

cmd=['send-data', '-f', file_name]
res = subprocess.run(cmd,stdout=subprocess.PIPE, input=b'a')

I have no control over send-data utility and it creates file under /tmp directory with random name as shown below in the response string. 我无法控制send-data实用程序,它会在/ tmp目录下使用随机名称创建文件,如响应字符串所示。

'a)bort, e)dit or s)end? send-data: the file remains in /tmp/pbadLWX .'

Wondering how can i delete the file "pbadLWX" under /tmp folder after each execution? 想知道每次执行后如何删除/ tmp文件夹下的文件“ pbadLWX”? Considering every time file name could be different? 考虑每次文件名都可以不同吗?

You can parse the output via re module and then delete the file via os.remove : 您可以通过re模块解析输出,然后通过os.remove删除文件:

import os
import re

f_name = re.search(r'\s(/tmp/[a-zA-Z]+)\s', res.stdout.decode('ascii')).group(1)
os.remove(f_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM