【发布时间】:2021-03-01 15:00:49
【问题描述】:
我只想使用 Python 脚本在 Linux (Ubuntu) 中删除具有 777 权限的文件,但它会删除我创建的所有文件。
立即输出:脚本正在发送有关文件创建的通知,并在脚本运行时删除所有文件。
预期输出:当用户创建具有 777 权限的文件然后将其删除时,脚本会发送通知。不接触其他文件,例如只读取权限文件。
import os
from time import sleep
import subprocess
# folder = os.getcwd()
# folder = r'C:\Users\TheAlestGuy\Desktop\TestPu\Testing'
folder = "/home/alen/Desktop/TestFolder/Teting"
try:
while True:
files = os.listdir(folder)
for file in files:
if os.stat(folder).st_mode & 0o777:
os.remove(f'{folder}/{file}')
subprocess.Popen(['notify-send', "User is trying to create file with 777 permissios"])
sleep(5)
except KeyboardInterrupt:
print("Script terminated, no more files will be deleted :)")
pass
【问题讨论】:
标签: python python-3.x linux