python os.system() 没有以我的权限运行?

标签 python security permissions grep os.system

我正在尝试使用 os.system("grep 'regex'/path/to/file.log") 来 grep 文件。我知道正则表达式是正确的,并且该命令在从 shell 运行时有效,但是一旦我尝试运行脚本,就会收到以下错误:

sh: /path/to/file.log: Permission denied

os 模块是否未以我的权限运行?另外,有更好的方法吗?我需要在 40,000 多行文件中查找特定行。

谢谢!

最佳答案

使用 Python 内置的正则表达式模块 re 来完成此操作可能比使用 grep 更容易。

import re

with open(filename, 'r') as f:
    for line in f:
        if re.search(regex, line):
            print line,

我知道这并不能直接回答问题,但它可能是解决根本问题的正确解决方案。我认为这属于“有更好的方法吗?”的问题。您问题的一部分。

关于python os.system() 没有以我的权限运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13314664/

相关文章:

Android Studio 无法识别 checkSelfPermission

安卓权限。INTERACT_ACROSS_USERS_FULL

python - 只允许名称参数的函数

python - 在 python PyNaCl 中从数据库中检索加密 key ,我如何转换回 PublicKey 或 PrivateKey 对象?

security - 跨站点表单发布与不同域上的外部站点

c# - 是否可以通过其他应用程序使用 TrueCrypt? (C#)

android - 如何为 Gingerbread 上运行的应用授予 MODIFY_PHONE_STATE 权限

python - 使用 python 和 xpath 进行字符串连接

python - 在将 xml 文件提供给解析器之前如何忽略 xml 标记中的特殊字符

php - 高效安全的查询思路——mysql