kerln888 发表于 2022-5-23 09:26:51

linux暂停报错

代码如下:
import os

os.system('pause')

windows系统正常,能暂停
linux下报错:
    sh: pause: not found

请问一下,在linux这代码应该怎么改,谢谢各位大神

豆嘉木 发表于 2022-5-23 09:59:00

https://wenku.baidu.com/view/808db838c6da50e2524de518964bcf84b8d52d53.html
https://blog.csdn.net/u012759006/article/details/84819894

豆嘉木 发表于 2022-5-23 10:35:22

因为在Linux的shell里pause不是一条命令,所以你这么写肯定是不对的
有帮助的话给个最佳答案谢了!!!{:10_254:}

人造人 发表于 2022-5-23 10:41:08

$ ls
pause.c
$ cat pause.c
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <termios.h>

int main(void) {
    {
      struct termios termios;
      ioctl(STDIN_FILENO, TCGETS, &termios);
      termios.c_lflag &= ~(ICANON | ECHO);
      ioctl(STDIN_FILENO, TCSETS, &termios);
    }
    printf("Press any key to continue...");
    getchar();
    {
      struct termios termios;
      ioctl(STDIN_FILENO, TCGETS, &termios);
      termios.c_lflag |= ICANON | ECHO;
      ioctl(STDIN_FILENO, TCSETS, &termios);
    }
    putchar('\n');
    return 0;
}
$ gcc -Wall -O3 -o pause pause.c $(pkg-config --cflags --libs ncurses)
$ ls
pausepause.c
$ ./pause
Press any key to continue...
$ ./pause
Press any key to continue...
$ ./pause
Press any key to continue...
$ ./pause
Press any key to continue...
$ cp pause ~/bin
$ cd ~/tmp
$ cat main.py
#!/usr/bin/env python
#coding=utf-8

import os

print('hello world!')
os.system('pause')
print('hello world!')
$ ./main.py
hello world!
Press any key to continue...
hello world!
$ ./main.py
hello world!
Press any key to continue...
hello world!
$ ./main.py
hello world!
Press any key to continue...
hello world!
$
$
$
$ ./main.py
hello world!
Press any key to continue...
hello world!
$



人造人 发表于 2022-5-23 10:48:47

奇怪,编译命令怎么没有改过来

gcc -Wall -O3 -o pause pause.c

kerln888 发表于 2022-5-23 11:07:47

豆嘉木 发表于 2022-5-23 10:35
因为在Linux的shell里pause不是一条命令,所以你这么写肯定是不对的
有帮助的话给个最佳答案谢了!!!{:1 ...

这是C+的处理方法,我想要的是python的。用input('按任意键继续')可以模拟暂停效果。除了这个还有其他方法吗??大佬

人造人 发表于 2022-5-23 11:13:55

kerln888 发表于 2022-5-23 11:07
这是C+的处理方法,我想要的是python的。用input('按任意键继续')可以模拟暂停效果。除了这个还有其他 ...

你写一个pause命令不就可以了
管你用什么语言写,你用python写一个pause命令

你是要解决这个问题,你还要求用什么语言来解决吗?

kerln888 发表于 2022-5-23 11:14:10

人造人 发表于 2022-5-23 10:48
奇怪,编译命令怎么没有改过来

这是C+的处理方法吧,我想要的是python的。用input('按任意键继续')可以模拟暂停效果。除了这个还有其他方法吗??大神

人造人 发表于 2022-5-23 11:14:46

kerln888 发表于 2022-5-23 11:07
这是C+的处理方法,我想要的是python的。用input('按任意键继续')可以模拟暂停效果。除了这个还有其他 ...

嗯,看错人了,抱歉
^_^

人造人 发表于 2022-5-23 11:15:21

kerln888 发表于 2022-5-23 11:14
这是C+的处理方法吧,我想要的是python的。用input('按任意键继续')可以模拟暂停效果。除了这个还有其他 ...

好吧,没有看错

你写一个pause命令不就可以了
管你用什么语言写,你用python写一个pause命令

你是要解决这个问题,你还要求用什么语言来解决吗?

豆嘉木 发表于 2022-5-23 11:28:14

kerln888 发表于 2022-5-23 11:07
这是C+的处理方法,我想要的是python的。用input('按任意键继续')可以模拟暂停效果。除了这个还有其他 ...

这里有python的pause包,你去下载:https://www.cnpython.com/pypi/pause
别忘了最佳答案哈哈哈

人造人 发表于 2022-5-23 11:40:45

原理都一样,要学会举一反三

#!/usr/bin/env python
#coding=utf-8

import termios
import sys

tios = termios.tcgetattr(sys.stdin.fileno())
tios &= ~(termios.ICANON | termios.ECHO)
termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)

print('Press any key to continue...', end = '', flush = True)
sys.stdin.read(1)
print()

tios = termios.tcgetattr(sys.stdin.fileno())
tios |= termios.ICANON | termios.ECHO
termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)

人造人 发表于 2022-5-23 11:43:37

#!/usr/bin/env python
#coding=utf-8

import termios
import sys

tios = termios.tcgetattr(sys.stdin.fileno())
tios &= ~(termios.ICANON | termios.ECHO)
termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)

print('Press any key to continue...', end = '', flush = True)
sys.stdin.read(1)
print('', flush = True)

tios = termios.tcgetattr(sys.stdin.fileno())
tios |= termios.ICANON | termios.ECHO
termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)
页: [1]
查看完整版本: linux暂停报错