0
def os_check():
    if os.name == 'nt':
        write_file = r'D:\text.txt'
        return write_file
    else:
        write_file = '/folder/txttt.txt'
        return  write_file

os_check()

print(write_file)

When I try to return variable I have error:

NameError: name 'write_file' is not defined

How to return in properly?

  • 1
    Does this answer your question? [Python: Return possibly not returning value](https://stackoverflow.com/questions/53347740/python-return-possibly-not-returning-value) – dspencer Feb 05 '21 at 20:40
  • You don't return *variabls* you return *objects*. Your `os_check()` call returned that object, but you didn't assign it to anything, therefore the result is discarded, you needed something like `write_file = os_check()`, or any other variable, `foo = os_check()` – juanpa.arrivillaga Feb 05 '21 at 20:52

1 Answers1

1

You forgot to assign a value to a new variable

import  os
def os_check():
    if os.name == 'nt':
        write_file = r'D:\text.txt'
        return write_file
    else:
        write_file = '/folder/txttt.txt'
        return write_file

write_file = os_check()  # <--- here

print(write_file)
Arseniy
  • 680
  • 5
  • 10