首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么shopt在从python调用时不会失败?

为什么shopt在从python调用时不会失败?
EN

Stack Overflow用户
提问于 2015-09-15 18:54:40
回答 1查看 576关注 0票数 0
代码语言:javascript
复制
# shopt -s extglob

# python3
Python 3.4.0 (default, Sep  8 2015, 23:36:36) 
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import check_call
>>> check_call(['shopt', '-s', 'extglob'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/subprocess.py", line 552, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib/python3.4/subprocess.py", line 533, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib/python3.4/subprocess.py", line 848, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.4/subprocess.py", line 1446, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'shopt'

商店似乎不在我的路上,但巴什却在:

代码语言:javascript
复制
# echo $PATH | grep shopt
# whereis shopt
# whereis bash
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-15 21:13:39

如果您想要"rm目录中除了两个文件之外的所有文件“,您可以直接从Python调用shell。这个程序删除了/tmp/r中的所有文件,除了我想保留的两个文件。

代码语言:javascript
复制
#!/usr/bin/python3

import os

keepers = ['safe', 'also_safe.txt']
os.chdir('/tmp/r')

for filename in os.listdir('.'):
    if filename not in keepers:
        print('Removing %s' % (filename,))
        os.remove(filename)

而且,为了好玩,下面是一个提供相同功能的组合模块和脚本:

代码语言:javascript
复制
#!/usr/bin/python3

import os
import argparse
import sys

def safe_remove(dirs, exclude, verbose, dry_run):
    for directory in dirs:
        if verbose or dry_run:
            print("Checking directory '%s'" % (directory,))
        for filename in os.listdir(directory):
            if filename not in exclude:
                filename = os.path.join(directory, filename)
                if verbose or dry_run:
                    print('rm %s'%filename)
                if not dry_run:
                    os.remove(filename)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Remove files, with exceptions')
    parser.add_argument('-x', '--exclude',
                        metavar='FILE',
                        default=[],
                        nargs='+',
                        help='files to exclude')
    parser.add_argument('dirs',
                        metavar='DIR',
                        nargs='+',
                        help='directories to clean')
    parser.add_argument('-v', '--verbose',
                        action='store_true',
                        help='Print actions')
    parser.add_argument('-n', '--dry-run',
                        action='store_true',
                        help='Print, but do not perform, actions')
    args = parser.parse_args()
    safe_remove(**vars(args))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32593521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档