0

I am trying to rewrite a Perl script a coworker made that you lets you bookmark directories.

The way it works is you type go add dir while in a folder and from anywhere else in terminal you type go dir and you will go to the bookmarked directory.

From what I can tell go is a bash function that calls the perl script with the following code: cd (godir). It seems that to print out anything other than a dir it prints STDERR. When I use sys.stderr.write() in python it prints out the text but it also prints out cd: The directory 'None' does not exist

This is an edit because my original question wasn't specific enough. I actually found a solution: My fish function will eval whatever the Python script returns

eval (python go.py $argv)
Daxter304
  • 11
  • 1
  • I apologize, I was in a hurry and did not provide specific enough information... I'm going to edit the question with much better information – Daxter304 Nov 08 '15 at 04:03
  • I suspect, based on the added information, that your original problem was the different behavior of `print` in Perl and Python when given a "nonexistent" value: in Perl, `print undef` will print nothing, whereas in Python, `print None` will, in fact, print the string `None`. Glad to see you found a work-around, though. – Ilmari Karonen Nov 08 '15 at 14:56

2 Answers2

4

In Python 2 you can do:

print >> sys.stderr, """Text to print"""

For Python 3 the syntax is:

print("""Text to print""", file=sys.stderr)

Of course import sys is required in both cases to import the sys module.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0

This was answered here

I found this to be the only one short + flexible + portable + readable:

from __future__ import print_function ...

def warning(*objs): print("WARNING: ", *objs, file=sys.stderr)

Community
  • 1
  • 1
dunck
  • 398
  • 1
  • 5
  • 18
  • 1
    Hi dvdt, welcome to Stack Overflow. If you ever find a question that can be answered by copy-pasting another Stack Overflow answer verbatim, please flag the question as a duplicate by clicking the "flag" button beneath the question. Marking questions as duplicates makes it easier for users to find them when searching. – ThisSuitIsBlackNot Nov 08 '15 at 03:55