python - How can I call a variable from another file? -


i have 2 files. first file, we'll call "main.py". second, "file1.py".

i want call variable main.py , write value new file called "tempfile.txt". i've tried importing "main.py" attritbute error.

here's example of "file1.py"

import os import sys import main   # function should write values main.py tempfile    # , reads contents store list.  def writevalues():      tempfile = open('tempfile.txt', 'w+')             tempfile.write(str(x_value))     tempfile.write("\n")     tempfile.write(str(y_value))     zonevalues = [line.rstrip('\n') line in open('tempfile.txt')]     print zonevalues   # x_value , y_value variables in main.py trying access    def readzonevalues(): # creates list values in tempfile.txt     valueslist = [line.rstrip('\n') line in open('tempfile.txt')]     print valueslist 

i've tried other looking answers there no clear answer specific issue.

edit:

main.py

 import os  import sys  import file1     x_value = 1000  y_value = 1000     # statement manipulates values, long post.  "something":      if "something":    # after values calculated, kick console  print "x value: " + str(x_value) + "\n"  print "y value: " + str(y_value) + "\n" 

i need values of variables written tempfile after main.py has been processed.

edit:

i have tried having tempfile created in main.py, reason function reading tempfile , adding values list not appear, however, values appear after delete tempfile creation in main.py , uncomment write function in file1.py

the code you're presenting creates circular import; i.e. main.py imports file1.py , file1.py imports main.py. doesn't work. recommend changing write_values() accept 2 parameters, , passing them in main.py, , eliminating import of main file1:

main.py:

import os import sys import file1  x_value = 1000 y_value = 1000  file1.writevalues(x_value, y_value) 

file1.py:

import os import sys  # function should write values main.py tempfile  # , reads contents store list. def writevalues(x_value, y_value):     tempfile = open('tempfile.txt', 'w+')             tempfile.write(str(x_value))     tempfile.write("\n")     tempfile.write(str(y_value))     tempfile.close()     zonevalues = [line.rstrip('\n') line in open('tempbeds.txt')]     print zonevalues  def readzonevalues(): # creates list values in tempfile.txt     valueslist = [line.rstrip('\n') line in open('tempfile.txt')]     print valueslist 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -