![PY 파일을 통해 Bash 명령을 실행하는 방법은 무엇입니까? [반복하다]](https://linux55.com/image/151252/PY%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%ED%86%B5%ED%95%B4%20Bash%20%EB%AA%85%EB%A0%B9%EC%9D%84%20%EC%8B%A4%ED%96%89%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F%20%5B%EB%B0%98%EB%B3%B5%ED%95%98%EB%8B%A4%5D.png)
/usr/lib/python2.7/dist-packages/Mailnag/plugins/libnotifyplugin.py
다음 Python 스크립트는 명령이 ('zenity --info --text="You got new mail"')
실행되지 않는 스크립트에서 추출됩니다 . 어떻게 작동하게 합니까? mail_count > 0
아래 줄이 작동 if mail_count > 1:
하면 실행되기를 원합니다 .
if mail_count > 0:
import os
os.system('zenity --info --text="You got new mail"')
if mail_count > 1:
summary = _("{0} new mails").format(str(mail_count))
if (mail_count - i) > 1:
body = _("from {0} and others.").format(senders)
else:
body = _("from {0}.").format(senders)
else:
summary = _("New mail")
body = _("from {0}.").format(senders)
다음 "Testing.py" 스크립트를 넣고 /usr/lib/python2.7/dist-packages/Mailnag/plugins/Testing.py
실행했는데 실제로 "새 메일이 있습니다"라는 메시지가 표시되었습니다.
#!/usr/bin/env python2
import os
os.system('zenity --info --text="You got new mail"')
답변1
sys 명령의 출력을 캡처할 필요가 없으므로 os.system
괜찮습니다.
올려주신 코드는 괜찮은 것 같으니, zenity
python 스크립트가 실행되는 경로에서 실행 중인 명령어가 사용 가능한지 추측해 보세요.
출력이 유효한지 확인하려면 다음을 수행하십시오.
if mail_count > 0:
import os
result = os.system('echo "more than 0" > testfile')
if result == 0:
print("a testfile was created")
명령이 테스트 파일을 생성하면 문제가 해결되었음을 알 수 있습니다 zenity
.
노트
이것은좋은 연습, import
문장은 가능하면 문서의 시작 부분에 배치하는 것이 가장 좋습니다.