Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Dave Laffan
Dave Laffan
4,604 Points

Using a clear_screen() in PyCharm

Hey

I'm following the Python track and a few times it creates a small function to clear the screen, which is

def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')

However I'm using PyCharm and instead of clearing the screen, the run window just adds a funny little symbol to the start of any output, and doesn't clear the screen.

It's almost like PyCharm is saying "I know you want to clear the screen, and I won't let you, but I'll add this little symbol to show you I know you have asked!"

This question has been raised before, about 5 months ago, but was never solved. If someone could help please?

Thanks

3 Answers

The os.system command only runs commands in the Console. It won't work in many other places including IDLE's Python Shell, PyCharm, etc.

It should work in workspaces or in your Terminal (or PowerShell if you are on windows). :smile:

~Alex

Dave Laffan
Dave Laffan
4,604 Points

Thanks Alex - is there an equivalent command or similar I can use in PyCharm to clear the run screen when coded, rather than pressing a button each time?

I know it's hardly the most important thing, but it's little things like this that bug the hell out of me :smile:

You can write your code in PyCharm then switch over to a Terminal on Mac/Linux and run the program.

Dave Laffan
Dave Laffan
4,604 Points

Very true. When I read your answer, the first thing that came to mind was "Well that's a bloody pain", but once I'd gotten over myself I realised, it actually makes perfect sense and I'm just being picky lol. Thank you :smile:

No problem :)