2

I’m using the Windows version of Python 2.7 with IDLE. If I run the following code

import os
os.getcwd()

through IDLE (Run module F5), I get no output in the Python shell. If I double-click on test.py in Explorer, however, the current working directory is displayed.

If I do a print command in IDLE, it shows up.

Why doesn’t os.getcwd() have any output in IDLE, while print does?

3
  • os.getcwd() returns a string, to print that string use print. Commented Aug 3, 2013 at 14:06
  • Well I did notice u can use print. But why in a python shell the returned string is showed without using print and in IDLE it doesn't? Commented Aug 3, 2013 at 14:22
  • That's because python shell echoes the return value value(repr version) of an expression. Commented Aug 3, 2013 at 14:29

1 Answer 1

5

When you call a function, that function may return a value, and in this case os.getcwd() returns a string. Here, you never do anything to that string, so nothing happens - there's no output because you never print the string.

For example,

print os.getcwd()

would output what you would expect.

Reading your comment above, most Python interpreters will print out a representation of the return value of your code after it has been interpreted. This is why you see the string "printed" in the Python shell. However, when actually running the code, return values do not output.

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

Comments

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.