Cannot Launch Interactive Program While Piping to Script in Python

后端 未结 2 674
臣服心动
臣服心动 2021-01-15 06:34

I have a python script that needs to call the defined $EDITOR or $VISUAL. When the Python script is called alone, I am able to launch the $ED

2条回答
  •  不知归路
    2021-01-15 07:01

    The specific case of find -type f | vidir - is handled here:

    foreach my $item (@ARGV) {
        if ($item eq "-") {
            push @dir, map { chomp; $_ } ;
            close STDIN;
            open(STDIN, "/dev/tty") || die "reopen: $!\n";
        }
    

    You can re-create this behavior in Python, as well:

    #!/usr/bin/python
    
    import os
    import sys
    
    sys.stdin.close()
    o = os.open("/dev/tty", os.O_RDONLY)
    os.dup2(o, 0)
    os.system('vim')
    

    Of course, it closes the standard input file descriptor, so if you intend on reading from it again after starting the editor, you should probably duplicate its file descriptor before closing it.

提交回复
热议问题