简体   繁体   English

为什么从 python 调用 shopt 会失败?

[英]Why does shopt fail when called from python?

# 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'

shopt doesn't appear to be in my path but bash does: shopt 似乎不在我的路径中,但 bash 确实:

# echo $PATH | grep shopt
# whereis shopt
# whereis bash
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

If you want to " rm all but two files in a directory ", you can do that directly from Python without invoking a shell.如果您想“ rm 目录中除两个文件之外的所有文件”,您可以直接从 Python 执行此操作,而无需调用 shell。 This program removes all of the files in /tmp/r , except for two that I want to keep.该程序删除了/tmp/r中的所有文件,除了我想保留的两个文件。

#!/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)

And, for fun, here is a combined module and script that provide the same functionality:而且,为了好玩,这里有一个组合模块和脚本,提供相同的功能:

#!/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))

暂无
暂无

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

相关问题 为什么我的Python程序在从Java调用时会失败? - Why does my Python program fail when called from Java? 为什么从PHP调用的长时间运行的Python脚本失败 - Why does a long running Python script called from PHP fail 从Python 2移植到Python 3时,为什么ord()失败? - Why does ord() fail when porting from Python 2 to Python 3? 为什么Excel宏在Excel中起作用,但从Python调用时却不能起作用? - Why does Excel macro work in Excel but not when called from Python? 为什么 tar 在使用 * 通配符时通过 Python 的 subprocess.call 调用时无法创建存档? - Why does tar fail to create an archive when called via Python's subprocess.call while using a * wildcard? 从我自己的目录运行Python时,为什么Python无法运行该程序? - Why does Python fail to run a program when I run it from its own directory? 为什么此python脚本仅在从命令行运行时才会失败? - Why does this python script only fail when run from the command line? 当我从目录外部导入此函数时,为什么我的python import语句失败? - Why does my python import statement fail when I import this function from outside the directory? 当我从 SQL 服务器代理运行时,为什么我的 Python 脚本会失败? - Why does my Python script fail when I run it from SQL Server Agent? 为什么在打印时比较这两个日期在python中失败 - Why does comparing these 2 dates fail in python when printed they are the same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM