鱼C论坛

 找回密码
 立即注册
查看: 2120|回复: 12

[已解决]linux暂停报错

[复制链接]
发表于 2022-5-23 09:26:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码如下:
import os

os.system('pause')

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

请问一下,在linux这代码应该怎么改,谢谢各位大神
最佳答案
2022-5-23 11:43:37
  1. #!/usr/bin/env python
  2. #coding=utf-8

  3. import termios
  4. import sys

  5. tios = termios.tcgetattr(sys.stdin.fileno())
  6. tios[3] &= ~(termios.ICANON | termios.ECHO)
  7. termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)

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

  11. tios = termios.tcgetattr(sys.stdin.fileno())
  12. tios[3] |= termios.ICANON | termios.ECHO
  13. termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 09:59:00 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-23 10:35:22 | 显示全部楼层
因为在Linux的shell里pause不是一条命令,所以你这么写肯定是不对的
有帮助的话给个最佳答案谢了!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-23 10:41:08 | 显示全部楼层
  1. $ ls
  2. pause.c
  3. $ cat pause.c
  4. #include <stdio.h>
  5. #include <sys/ioctl.h>
  6. #include <unistd.h>
  7. #include <termios.h>

  8. int main(void) {
  9.     {
  10.         struct termios termios;
  11.         ioctl(STDIN_FILENO, TCGETS, &termios);
  12.         termios.c_lflag &= ~(ICANON | ECHO);
  13.         ioctl(STDIN_FILENO, TCSETS, &termios);
  14.     }
  15.     printf("Press any key to continue...");
  16.     getchar();
  17.     {
  18.         struct termios termios;
  19.         ioctl(STDIN_FILENO, TCGETS, &termios);
  20.         termios.c_lflag |= ICANON | ECHO;
  21.         ioctl(STDIN_FILENO, TCSETS, &termios);
  22.     }
  23.     putchar('\n');
  24.     return 0;
  25. }
  26. $ gcc -Wall -O3 -o pause pause.c $(pkg-config --cflags --libs ncurses)
  27. $ ls
  28. pause  pause.c
  29. $ ./pause
  30. Press any key to continue...
  31. $ ./pause
  32. Press any key to continue...
  33. $ ./pause
  34. Press any key to continue...
  35. $ ./pause
  36. Press any key to continue...
  37. $ cp pause ~/bin
  38. $ cd ~/tmp
  39. $ cat main.py
  40. #!/usr/bin/env python
  41. #coding=utf-8

  42. import os

  43. print('hello world!')
  44. os.system('pause')
  45. print('hello world!')
  46. $ ./main.py
  47. hello world!
  48. Press any key to continue...
  49. hello world!
  50. $ ./main.py
  51. hello world!
  52. Press any key to continue...
  53. hello world!
  54. $ ./main.py
  55. hello world!
  56. Press any key to continue...
  57. hello world!
  58. $
  59. $
  60. $
  61. $ ./main.py
  62. hello world!
  63. Press any key to continue...
  64. hello world!
  65. $
复制代码


1.gif
2.gif
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-23 10:48:47 | 显示全部楼层
奇怪,编译命令怎么没有改过来

  1. gcc -Wall -O3 -o pause pause.c
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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


这是C+的处理方法,我想要的是python的。用input('按任意键继续')可以模拟暂停效果。除了这个还有其他方法吗??大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-23 11:14:10 | 显示全部楼层
人造人 发表于 2022-5-23 10:48
奇怪,编译命令怎么没有改过来

这是C+的处理方法吧,我想要的是python的。用input('按任意键继续')可以模拟暂停效果。除了这个还有其他方法吗??大神
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

嗯,看错人了,抱歉
^_^
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

好吧,没有看错

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

你是要解决这个问题,你还要求用什么语言来解决吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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


这里有python的pause包,你去下载:https://www.cnpython.com/pypi/pause
别忘了最佳答案哈哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-23 11:40:45 | 显示全部楼层
原理都一样,要学会举一反三

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

  3. import termios
  4. import sys

  5. tios = termios.tcgetattr(sys.stdin.fileno())
  6. tios[3] &= ~(termios.ICANON | termios.ECHO)
  7. termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)

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

  11. tios = termios.tcgetattr(sys.stdin.fileno())
  12. tios[3] |= termios.ICANON | termios.ECHO
  13. termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-23 11:43:37 | 显示全部楼层    本楼为最佳答案   
  1. #!/usr/bin/env python
  2. #coding=utf-8

  3. import termios
  4. import sys

  5. tios = termios.tcgetattr(sys.stdin.fileno())
  6. tios[3] &= ~(termios.ICANON | termios.ECHO)
  7. termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)

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

  11. tios = termios.tcgetattr(sys.stdin.fileno())
  12. tios[3] |= termios.ICANON | termios.ECHO
  13. termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tios)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 00:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表