Hướng dẫn python clear screen - python rõ màn hình

Xem thảo luận

Show

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    ctrl+l

    Bàn luận

    Hầu hết thời gian, trong khi làm việc với Shell/thiết bị đầu cuối tương tác Python (không phải là một bảng điều khiển), chúng tôi kết thúc với đầu ra lộn xộn và muốn xóa màn hình vì một số lý do. Trong một shell/thiết bị đầu cuối tương tác, chúng ta chỉ có thể sử dụng

    Nhưng, điều gì sẽ xảy ra nếu chúng ta muốn xóa màn hình trong khi chạy tập lệnh Python? Thật không may, không có từ khóa hoặc chức năng/phương thức tích hợp để xóa màn hình. Vì vậy, chúng tôi làm điều đó một mình.

    Xóa màn hình trong hệ điều hành Windows

    Python3

    Phương pháp 1: Màn hình rõ ràng trong Python bằng CLS

    import os
    
    os.system('cls')
    
    0
    import os
    
    os.system('cls')
    
    1
    import os
    
    os.system('cls')
    
    2

    Bạn có thể chỉ đơn giản là CL CLS để xóa màn hình trong Windows.

    import osfrom os import system” but with that, you have to change system(‘clear’) to os.system(‘clear’). 

    Python3

    Ví dụ 2: Màn hình rõ ràng trong Python bằng cách sử dụng Clear

    Bạn cũng chỉ có thể nhập khẩu OS OS OS thay vì từ hệ thống nhập hệ điều hành, nhưng với điều đó, bạn phải thay đổi hệ thống (’rõ ràng) thành hệ điều hành (‘ rõ ràng). & NBSP;

    import os
    
    os.system('cls')
    
    3
    import os
    
    os.system('cls')
    
    4import
    import os
    
    os.system('cls')
    
    6

    import os
    
    os.system('cls')
    
    3
    import os
    
    os.system('cls')
    
    8import
    import os
    
    os.system('clear')
    
    0

    import os
    
    os.system('clear')
    
    1
    import os
    
    os.system('clear')
    
    2

    import os
    
    os.system('clear')
    
    3
    import os
    from time import sleep
    
    # Printing Some Text
    print("Hello, Welcome to Scaler!!")
    print("Screen will now be cleared in 4 Seconds")
    
    # Waiting for 4 seconds to clear the screen
    sleep(4)
    
    # Clearing the Screen
    os.system('cls')
    
    7
    import os
    
    os.system('clear')
    
    9

    import os
    
    os.system('clear')
    
    3
    import os
    
    os.system('clear')
    
    4
    import os
    
    os.system('clear')
    
    5
    import os
    
    os.system('clear')
    
    6
    import os
    
    os.system('clear')
    
    6
    import os
    
    os.system('clear')
    
    8
    import os
    
    os.system('clear')
    
    9

    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    5
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    6
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    7
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    8
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    9
    import os
    
    os.system('cls')
    
    2

    import os
    from time import sleep
    
    # Printing Some Text
    print(1)
    print(2)
    print(3)
    print(4)
    print(5)
    print("Screen will now be cleared in 5 Seconds")
    
    # Waiting for 5 seconds to clear the screen
    sleep(5)
    
    # Clearing the Screen
    os.system('clear')
    
    1
    import os
    from time import sleep
    
    # Printing Some Text
    print(1)
    print(2)
    print(3)
    print(4)
    print(5)
    print("Screen will now be cleared in 5 Seconds")
    
    # Waiting for 5 seconds to clear the screen
    sleep(5)
    
    # Clearing the Screen
    os.system('clear')
    
    2
    import os
    
    os.system('cls')
    
    2

    import os
    from time import sleep
    
    # Printing Some Text
    print(1)
    print(2)
    print(3)
    print(4)
    print(5)
    print("Screen will now be cleared in 5 Seconds")
    
    # Waiting for 5 seconds to clear the screen
    sleep(5)
    
    # Clearing the Screen
    os.system('clear')
    
    4

    import os from time import sleep # Printing Some Text print("Hello, Welcome to Scaler!!") print("Screen will now be cleared in 4 Seconds") # Waiting for 4 seconds to clear the screen sleep(4) # Clearing the Screen os.system('cls') 0import os from time import sleep # Printing Some Text print("Hello, Welcome to Scaler!!") print("Screen will now be cleared in 4 Seconds") # Waiting for 4 seconds to clear the screen sleep(4) # Clearing the Screen os.system('cls') 1import os os.system('clear') 6 import os from time import sleep # Printing Some Text print("Hello, Welcome to Scaler!!") print("Screen will now be cleared in 4 Seconds") # Waiting for 4 seconds to clear the screen sleep(4) # Clearing the Screen os.system('cls') 3import os os.system('cls') 1import os os.system('cls') 2

    import os
    from time import sleep
    
    # Printing Some Text
    print("Hello, Welcome to Scaler!!")
    print("Screen will now be cleared in 4 Seconds")
    
    # Waiting for 4 seconds to clear the screen
    sleep(4)
    
    # Clearing the Screen
    os.system('cls')
    
    0
    import os
    from time import sleep
    
    # Printing Some Text
    print("Hello, Welcome to Scaler!!")
    print("Screen will now be cleared in 4 Seconds")
    
    # Waiting for 4 seconds to clear the screen
    sleep(4)
    
    # Clearing the Screen
    os.system('cls')
    
    1
    import os
    
    os.system('clear')
    
    6
    import os
    from time import sleep
    
    # Printing Some Text
    print("Hello, Welcome to Scaler!!")
    print("Screen will now be cleared in 4 Seconds")
    
    # Waiting for 4 seconds to clear the screen
    sleep(4)
    
    # Clearing the Screen
    os.system('cls')
    
    3
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    3
    import os
    
    os.system('cls')
    
    2subprocess module

    Python3

    Ví dụ 3: Xóa màn hình trong Python bằng cách sử dụng cuộc gọi

    Bạn cũng chỉ có thể nhập khẩu OS OS OS thay vì từ hệ thống nhập hệ điều hành, nhưng với điều đó, bạn phải thay đổi hệ thống (’rõ ràng) thành hệ điều hành (‘ rõ ràng). & NBSP;

    import os
    
    os.system('cls')
    
    3
    import os
    
    os.system('cls')
    
    4import
    import os
    
    os.system('cls')
    
    6

    import os
    
    os.system('cls')
    
    3
    import os
    
    os.system('cls')
    
    8import
    import os
    
    os.system('clear')
    
    0

    import os
    
    os.system('clear')
    
    1
    import os
    
    os.system('clear')
    
    2

    import os
    from time import sleep
    
    # Printing Some Text
    print(1)
    print(2)
    print(3)
    print(4)
    print(5)
    print("Screen will now be cleared in 5 Seconds")
    
    # Waiting for 5 seconds to clear the screen
    sleep(5)
    
    # Clearing the Screen
    os.system('clear')
    
    1
    import os
    from time import sleep
    
    # Printing Some Text
    print(1)
    print(2)
    print(3)
    print(4)
    print(5)
    print("Screen will now be cleared in 5 Seconds")
    
    # Waiting for 5 seconds to clear the screen
    sleep(5)
    
    # Clearing the Screen
    os.system('clear')
    
    2
    import os
    
    os.system('cls')
    
    2

    import os
    from time import sleep
    
    # Printing Some Text
    print(1)
    print(2)
    print(3)
    print(4)
    print(5)
    print("Screen will now be cleared in 5 Seconds")
    
    # Waiting for 5 seconds to clear the screen
    sleep(5)
    
    # Clearing the Screen
    os.system('clear')
    
    4

    import os os.system('clear') 3import os os.system('clear') 4 import os os.system('clear') 5import os os.system('clear') 6import os os.system('clear') 6 import os os.system('clear') 8import os os.system('clear') 9

    import os
    from time import sleep
    
    # Printing Some Text
    print("Hello, Welcome to Scaler!!")
    print("Screen will now be cleared in 4 Seconds")
    
    # Waiting for 4 seconds to clear the screen
    sleep(4)
    
    # Clearing the Screen
    os.system('cls')
    
    0
    import os
    from time import sleep
    
    # Printing Some Text
    print("Hello, Welcome to Scaler!!")
    print("Screen will now be cleared in 4 Seconds")
    
    # Waiting for 4 seconds to clear the screen
    sleep(4)
    
    # Clearing the Screen
    os.system('cls')
    
    1
    import os
    
    os.system('clear')
    
    6
    import os
    from time import sleep
    
    # Printing Some Text
    print("Hello, Welcome to Scaler!!")
    print("Screen will now be cleared in 4 Seconds")
    
    # Waiting for 4 seconds to clear the screen
    sleep(4)
    
    # Clearing the Screen
    os.system('cls')
    
    3
    import os
    
    os.system('cls')
    
    1
    import os
    
    os.system('cls')
    
    2

    Python3

    Phương pháp 1: Màn hình rõ ràng trong Python bằng CLS

    Bạn cũng chỉ có thể nhập khẩu OS OS OS thay vì từ hệ thống nhập hệ điều hành, nhưng với điều đó, bạn phải thay đổi hệ thống (’rõ ràng) thành hệ điều hành (‘ rõ ràng). & NBSP;

    import os
    
    os.system('cls')
    
    3
    import os
    
    os.system('cls')
    
    4import
    import os
    
    os.system('cls')
    
    6

    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    5
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    6
    import os
    
    os.system('cls')
    
    00
    import os
    
    os.system('cls')
    
    2

    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    5
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    6
    import os
    
    os.system('cls')
    
    04
    import os
    
    os.system('cls')
    
    2

    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    5
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    6
    import os
    
    os.system('cls')
    
    08
    import os
    
    os.system('cls')
    
    2

    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    5
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    6
    import os
    
    os.system('cls')
    
    12
    import os
    
    os.system('cls')
    
    2

    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    5
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    6
    import os
    
    os.system('cls')
    
    16
    import os
    
    os.system('cls')
    
    2

    import os
    from time import sleep
    
    # Printing Some Text
    print(1)
    print(2)
    print(3)
    print(4)
    print(5)
    print("Screen will now be cleared in 5 Seconds")
    
    # Waiting for 5 seconds to clear the screen
    sleep(5)
    
    # Clearing the Screen
    os.system('clear')
    
    1
    import os
    
    os.system('cls')
    
    19
    import os
    
    os.system('cls')
    
    2

    import os
    
    os.system('cls')
    
    0
    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    
    3
    import os
    
    os.system('cls')
    
    2


    Tổng quan

    Mô -đun OS Python được nhập để xóa màn hình bảng điều khiển trong bất kỳ hệ điều hành nào. Phương thức hệ thống () của mô -đun HĐH với chuỗi CL hoặc xóa dưới dạng tham số được sử dụng để xóa màn hình trong Windows và MacOS/Linux, tương ứng.os module is imported to clear the console screen in any operating system. The system() method of the os module with the string cls or clear as a parameter is used to clear the screen in windows and macOS/Linux, respectively.os module is imported to clear the console screen in any operating system. The system() method of the os module with the string cls or clear as a parameter is used to clear the screen in windows and macOS/Linux, respectively.

    Nội dung chính ShowShow

    • Tổng quan
    • Mô -đun OS Python được nhập để xóa màn hình bảng điều khiển trong bất kỳ hệ điều hành nào. Phương thức hệ thống () của mô -đun HĐH với chuỗi CL hoặc xóa dưới dạng tham số được sử dụng để xóa màn hình trong Windows và MacOS/Linux, tương ứng.os module is imported to clear the console screen in any operating system. The system() method of the os module with the string cls or clear as a parameter is used to clear the screen in windows and macOS/Linux, respectively.
    • Nội dung chính Show
    • Giới thiệu về màn hình Clear Python
    • Làm thế nào để xóa màn hình bảng điều khiển Python?
    • Ví dụ về màn hình Clear Python
    • Ví dụ 1: Xóa màn hình trong hệ điều hành Windows
    • Ví dụ 4: Điều gì sẽ xảy ra nếu chúng ta không biết chúng ta đang làm việc gì?
    • Sự kết luận
    • Đọc thêm
    • Có chức năng rõ ràng trong Python?
    • Làm thế nào để bạn sử dụng CLRSCR trong Python?
    • Làm thế nào để bạn xóa lịch sử Python?
    • Làm thế nào để bạn xóa văn bản in trong python?

    Trong Python, bạn có thể sử dụng các phương thức thay thế () và dịch () để chỉ định ký tự nào bạn muốn xóa khỏi chuỗi và trả về kết quả chuỗi được sửa đổi mới.use the replace() and translate() methods to specify which characters you want to remove from a string and return a new modified string result.

    • Tổng quan
    • Mô -đun OS Python được nhập để xóa màn hình bảng điều khiển trong bất kỳ hệ điều hành nào. Phương thức hệ thống () của mô -đun HĐH với chuỗi CL hoặc xóa dưới dạng tham số được sử dụng để xóa màn hình trong Windows và MacOS/Linux, tương ứng.os module is imported to clear the console screen in any operating system. The system() method of the os module with the string cls or clear as a parameter is used to clear the screen in windows and macOS/Linux, respectively.os module and its system() method.
    • Nội dung chính Showclear and cls commands and how to use them as a string parameter to the system() method.

    Mô -đun OS Python được nhập để xóa màn hình bảng điều khiển trong bất kỳ hệ điều hành nào. Phương thức hệ thống () của mô -đun HĐH với chuỗi CL hoặc xóa dưới dạng tham số được sử dụng để xóa màn hình trong Windows và MacOS/Linux, tương ứng.os module is imported to clear the console screen in any operating system. The system() method of the os module with the string cls or clear as a parameter is used to clear the screen in windows and macOS/Linux, respectively.

    Nội dung chính Showstudent 1, then after some time information of student 2 will be displayed and then finally the information of student 3 will be displayed. So, in that case, we will have to clear the python console each time after displaying information about each student.

    Giải thích hình ảnh của ví dụ trên

    Trong một shell/thiết bị đầu cuối tương tác, để xóa bảng điều khiển Python, chúng ta có thể sử dụng lệnh Ctrl+L, nhưng trong hầu hết các trường hợp, chúng ta phải xóa màn hình trong khi chạy tập lệnh Python, vì vậy chúng ta phải thực hiện lập trình.shell/terminal, to clear the python console, we can use the ctrl+l command, but in most cases, we have to clear the screen while running the python script, so we have to do it programmatically.shell/terminal, to clear the python console, we can use the ctrl+l command, but in most cases, we have to clear the screen while running the python script, so we have to do it programmatically.

    Chúng ta có thể xóa màn hình theo chương trình và nó giúp chúng ta định dạng đầu ra theo cách chúng ta muốn. Chúng tôi cũng có thể xóa đầu ra bất cứ khi nào chúng tôi muốn nhiều lần.clear screen programmatically, and it helps us to format the output in the way we want. We can also clear the output whenever we want many numbers of times.clear screen programmatically, and it helps us to format the output in the way we want. We can also clear the output whenever we want many numbers of times.

    Làm thế nào để xóa màn hình bảng điều khiển Python?

    Xóa bảng điều khiển trong Python có các phương pháp khác nhau cho các hệ điều hành khác nhau. Những điều này được nêu dưới đây:

    • Trong Windows: Để xóa bảng điều khiển trong hệ điều hành Windows, chúng tôi sẽ sử dụng hàm hệ thống () khỏi mô -đun HĐH với tham số 'CLS'. For clearing the console in the windows operating system, we will use the system() function from the os module with the 'cls' parameter. For clearing the console in the windows operating system, we will use the system() function from the os module with the 'cls' parameter.

    Cú pháp của hàm hệ thống (): Hàm System () nằm trong mô -đun HĐH, do đó, mô -đun HĐH cần được nhập trước khi sử dụng hàm System () trong Windows. Sau khi nhập mô -đun HĐH, tham số chuỗi 'CLS' được truyền bên trong hàm hệ thống để xóa màn hình. system() function is in the os module, so the os module needs to be imported before using the system() function in Windows. After importing the os module, the string parameter 'cls' is passed inside the system function to clear the screen. system() function is in the os module, so the os module needs to be imported before using the system() function in Windows. After importing the os module, the string parameter 'cls' is passed inside the system function to clear the screen.

    import os
    
    os.system('cls')
    
    • Trong Linux và MacOS: Để xóa bảng điều khiển trong các hệ điều hành Linux và Mac, chúng tôi sẽ sử dụng hàm hệ thống () từ mô -đun HĐH với tham số 'Xóa'. For clearing the console in Linux and Mac operating systems, we will use the system() function from the os module with the 'clear' parameter. For clearing the console in Linux and Mac operating systems, we will use the system() function from the os module with the 'clear' parameter.

    Cú pháp: Mô -đun HĐH cần được nhập trước khi sử dụng hàm hệ thống () trong Linux. Sau khi nhập tham số giá trị chuỗi mô -đun OS 'Xóa' được truyền bên trong hàm hệ thống để xóa màn hình. os module needs to be imported before using the system() function in Linux. After importing the os module string value parameter 'clear' is passed inside the system function to clear the screen. os module needs to be imported before using the system() function in Linux. After importing the os module string value parameter 'clear' is passed inside the system function to clear the screen.

    import os
    
    os.system('clear')
    
    • CTRL+L: Phương pháp này chỉ hoạt động cho hệ điều hành Linux. Trong một shell/thiết bị đầu cuối tương tác, chúng ta chỉ có thể sử dụng Ctrl+L để xóa màn hình. This method only works for Linux operating system. In an interactive shell/terminal, we can simply use ctrl+l to clear the screen. This method only works for Linux operating system. In an interactive shell/terminal, we can simply use ctrl+l to clear the screen.

    Ví dụ về màn hình Clear Python

    Ví dụ 1: Xóa màn hình trong hệ điều hành Windows

    Chúng ta hãy xem một ví dụ về màn hình rõ ràng trong Python để làm rõ sự hiểu biết của chúng ta.

    Chúng tôi sẽ sử dụng hàm hệ thống đã nêu () đã nêu để xóa bảng điều khiển Python.system() function for clearing the python console.system() function for clearing the python console.

    Đầu tiên, chúng tôi sẽ in một số đầu ra; Sau đó, chúng tôi sẽ đợi 4 giây bằng hàm Sleep () để tạm dừng chương trình trong 4 giây và sau đó, chúng tôi sẽ áp dụng hệ thống Os.System ('CLS') để xóa màn hình. sleep() function to halt the program for 4 seconds, and after that, we will apply os.system('cls') to clear the screen. sleep() function to halt the program for 4 seconds, and after that, we will apply os.system('cls') to clear the screen.

    Code:

    import os
    from time import sleep
    
    # Printing Some Text
    print("Hello, Welcome to Scaler!!")
    print("Screen will now be cleared in 4 Seconds")
    
    # Waiting for 4 seconds to clear the screen
    sleep(4)
    
    # Clearing the Screen
    os.system('cls')
    

    Output:

    Cửa sổ đầu ra sẽ in văn bản đã cho trước, sau đó chương trình sẽ ngủ trong 4 giây, sau đó màn hình sẽ bị xóa và việc thực hiện chương trình sẽ bị dừng.

    Ví dụ 2: Xóa màn hình, sau đó in một số thông tin khác trong hệ điều hành Windows.

    Đầu tiên chúng tôi sẽ in một số đầu ra, sau đó chúng tôi sẽ đợi 1 giây bằng hàm Sleep () và sau đó, chúng tôi sẽ áp dụng hệ điều hành ('CLS') để xóa màn hình.

    Sau đó, chúng tôi sẽ in thêm một số thông tin.

    Code:

    import os
    from time import sleep
    
    # Printing Some Text
    print("Name: ABC")
    print("Roll No: 1")
    print("Marks: 91")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: DEF")
    print("Roll No: 2")
    print("Marks: 81")
    
    # Waiting for 1 second to clear the screen
    sleep(1)
    
    # Clearing the Screen
    os.system('cls')
    
    # Printing Some Text
    print("Name: GHI")
    print("Roll No: 3")
    print("Marks: 93")
    
    sleep(1)
    
    
    

    Output:

    Cửa sổ đầu ra sẽ in thông tin đầu tiên, sau đó chương trình sẽ ngủ trong 1 giây và màn hình sẽ được xóa và sau đó nó sẽ in thông tin thứ hai. Một lần nữa, chương trình sẽ ngủ trong 1 giây và cuối cùng, chương trình sẽ bị dừng sau khi in lô thông tin cuối cùng.

    Ví dụ 3: Xóa màn hình trong hệ điều hành Linux

    Chúng tôi sẽ sử dụng phương thức hệ thống đã nêu ở trên () để xóa bảng điều khiển Python.system() method for clearing the python console.system() method for clearing the python console.

    Đầu tiên, chúng tôi sẽ in một số đầu ra; Sau đó, chúng tôi sẽ đợi trong 5 giây bằng hàm Sleep () và sau đó, chúng tôi sẽ áp dụng hệ điều hành ('xóa') để xóa màn hình.

    Code:

    import os
    from time import sleep
    
    # Printing Some Text
    print(1)
    print(2)
    print(3)
    print(4)
    print(5)
    print("Screen will now be cleared in 5 Seconds")
    
    # Waiting for 5 seconds to clear the screen
    sleep(5)
    
    # Clearing the Screen
    os.system('clear')
    

    Output:

    1
    2
    3
    4
    5
    The screen will now be cleared in 5 Seconds
    
    • Sau 5 giây, màn hình được xóa.

    Cửa sổ đầu ra sẽ in văn bản đã cho trước, sau đó chương trình sẽ ngủ trong 5 giây, sau đó màn hình sẽ bị xóa và việc thực hiện chương trình sẽ bị dừng.

    Ví dụ 4: Điều gì sẽ xảy ra nếu chúng ta không biết chúng ta đang làm việc gì?

    Có thể có một trường hợp trước tiên chúng ta phải xác định hệ điều hành mà chúng ta đang làm việc. Vì vậy, trước tiên chúng tôi sẽ xác định xem HĐH là Windows, Linux hoặc Mac.OS we are working on. So, we will first determine whether the os is Windows, Linux, or Mac.OS we are working on. So, we will first determine whether the os is Windows, Linux, or Mac.

    Đối với Windows, tên HĐH là "NT" và đối với Linux hoặc Mac, tên HĐH là "POSIX".

    Vì vậy, chúng tôi sẽ kiểm tra tên HĐH và sau đó áp dụng chức năng.

    Code:

    import os
    from time import sleep
    
    # Printing the os name
    print("os name is :", os.name)
    
    # Waiting for 2 seconds to clear the screen
    sleep(2)
    
    # Clearing the Screen
    # posix is os name for Linux or mac
    if(os.name == 'posix'):
       os.system('clear')
    # else screen will be cleared for windows
    else:
       os.system('cls')
    

    Output:

    Đối với trường hợp này, tên HĐH là NT, do đó, bảng điều khiển Windows sẽ bị xóa sau 2 giây ngủ chương trình, và sau đó nó sẽ bị chấm dứt.nt, so windows console will be cleared after 2 seconds of program sleep, and then it will be terminated.nt, so windows console will be cleared after 2 seconds of program sleep, and then it will be terminated.

    Sự kết luận

    Bây giờ chúng ta đã thấy nhiều ví dụ khác nhau về cách xóa màn hình trong Python, chúng ta hãy ghi chú một vài điểm.

    • Màn hình xóa có thể được thực hiện theo chương trình, và nó cũng giúp chúng tôi định dạng đầu ra theo cách chúng tôi muốn.
    • Để xóa bảng điều khiển trong hệ điều hành Windows, chúng tôi sẽ sử dụng phương thức hệ thống () từ mô -đun HĐH với tham số ‘CLS.system() method from the os module with the ‘cls’ parameter.system() method from the os module with the ‘cls’ parameter.
    • Để xóa bảng điều khiển trong hệ điều hành Linux, chúng tôi sẽ sử dụng phương thức hệ thống () từ mô -đun HĐH với tham số ‘rõ ràng.system() method from the os module with the ‘clear’ parameter.system() method from the os module with the ‘clear’ parameter.
    • Đối với Windows, tên HĐH là NT và đối với Linux, đó là POSIX, vì vậy trước tiên chúng tôi cũng có thể xác định HĐH và sau đó xóa màn hình.nt, and for Linux, it is posix, so we can also first determine the os and then clear the screen.nt, and for Linux, it is posix, so we can also first determine the os and then clear the screen.

    Đọc thêm

    1- Cái gì rõ ràng () trong Python?

    2 - Làm thế nào để cài đặt Python trong Linux?

    Có chức năng rõ ràng trong Python?

    Từ điển python rõ ràng () Phương thức rõ ràng () loại bỏ tất cả các mục khỏi từ điển.Tham số: Phương thức rõ ràng () không lấy bất kỳ tham số nào.The clear() method removes all items from the dictionary. Parameters: The clear() method doesn't take any parameters.The clear() method removes all items from the dictionary. Parameters: The clear() method doesn't take any parameters.

    Làm thế nào để bạn sử dụng CLRSCR trong Python?

    Trong Python đôi khi chúng tôi có liên kết đầu ra và chúng tôi muốn xóa màn hình trong dấu nhắc ô, chúng tôi có thể xóa màn hình bằng cách nhấn điều khiển + L.pressing Control + l .pressing Control + l .

    Làm thế nào để bạn xóa lịch sử Python?

    Các lệnh được sử dụng để xóa thiết bị đầu cuối hoặc vỏ python là CLS và rõ ràng.cls and clear.cls and clear.

    Làm thế nào để bạn xóa văn bản in trong python?

    Trong Python, bạn có thể sử dụng các phương thức thay thế () và dịch () để chỉ định ký tự nào bạn muốn xóa khỏi chuỗi và trả về kết quả chuỗi được sửa đổi mới.use the replace() and translate() methods to specify which characters you want to remove from a string and return a new modified string result.use the replace() and translate() methods to specify which characters you want to remove from a string and return a new modified string result.