-1

I have a function:

def hello_world(output):
    output.write("Hello World")

And would like to pass these to it:

file = io.open("myfile.txt", "w")
hello_world(file)
hello_world(sys.stdout)

I don't know if it is right this way. I also get NameError: global name 'sys' is not defined How would you do what I would like to?

gen
  • 9,528
  • 14
  • 35
  • 64

5 Answers5

3

Try to add:

import sys

to the top of your file

M1Reeder
  • 692
  • 2
  • 7
  • 22
2

NameError: global name 'sys' is not defined

Fixed by import sys at the top of your python file

As an aside, it's a horrible idea to have variables named file or list or dict or pretty much any default type.

Community
  • 1
  • 1
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
1

You should add this line at the beginning of your script:

import sys
nio
  • 5,141
  • 2
  • 24
  • 35
1

you forgot to import sys

sys is the "System-specific parameters and functions" module of python - python docs.

what this does is get the system specific standard out pipe for the python file to output to. as in a stream it can write to :).

File and sys.stdout, sys.stdin and sys.stderr are all just streams to write to.

In general if it says NameError you either forgot to import something or you didn't define something correctly :)

you probably want to rewrite that as opened_file or the like, File and some other words similar like if you did random = 3 are never good to address because they are used as names of various Python objects or internals - and you want to make sure you don't have collisions even though Python is supposed to resolve these cleverly :)

Eiyrioü von Kauyf
  • 4,481
  • 7
  • 32
  • 41
1

You need to import sys module:

import sys

Then you can use sys.stdout afterwards.

jh314
  • 27,144
  • 16
  • 62
  • 82