1

I have just started learning python. I am using python 2.7.5 I created the test module named "t.py" as below:

$cat > t.py
import os
def greeting(name):
  print("Hello, " + name)
c=os.system('clear')

When I am trying to use the module in python c is not working but greeting is working

$python
>>> t.greeting("test")
Hello, test
>>> t.c
0

Can you please advise why t.c is not clearing the screen within the python.Thanks.

2
  • 2
    If you've just started learning Python, you should definitely use the most recent version - 3.7 - not the very-out-of-date 2.7. Commented Aug 20, 2018 at 10:35
  • Thanks Daniel Roseman , the server I am using has the version 2.7.5 installed. There might be future work on python 2.7.5 so I am getting acquainted with it. I will try to install the version 3.7 on my home computer though thanks for the suggestion :) Commented Aug 20, 2018 at 10:41

1 Answer 1

9

Because you've set c as a variable (the result of one call of os.system('clear')), not a function. You'd have to do

def c():
   os.system('clear')

and then call

>>> t.c()

FYI, subprocess.call is preferred over os.system which only exists for backwards compatibility.

Sign up to request clarification or add additional context in comments.

2 Comments

Ohh...wow... thanks FHTMitchell for your help :) Plus 1 and will accept as the answer in 9 minutes due to the limit. :D
And thanks for subprocess tip too... >>> import subprocess >>> import subprocess as sp >>> sp.call('clear') 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.