1

I have a one.py as:

one.py

def file_save():
    f = th1.asksaveasfile(mode='w', defaultextension=".txt")
    filename = f.name

I have another file two.py where i need to open 'filename' from 'one.py':

from one import filename
with open(filename,'w'):
     print('hello')

please help me to fix the problem,its not getting filename.Answers will be appreciated!

3
  • Why should filename have a value if you never call file_save? Remember, functions don't do anything unless you execute them. Commented Aug 7, 2014 at 14:38
  • 1
    filename is assigned inside a function in one.py; why not make the function return it, then call the function? Commented Aug 7, 2014 at 14:38
  • @jonrsharpe one.py is an tkinter app,i need to write the name of the 'filename' to new text file,instead its again opening the tkinter gui rather than printing hello! Commented Aug 7, 2014 at 14:46

1 Answer 1

1

One.py:

def file_save():
    f = th1.asksaveasfile(mode='w', defaultextension=".txt")
    filename = f.name
    return filename;

Main.py:

from one import file_save
with open(file_save,'w'):
   print('hello')

In Python, you cannot access a variable in a function unless it is returned.

Edit:

Try

f = open(file_save, 'w')
print('hello')
Sign up to request clarification or add additional context in comments.

3 Comments

infact,i need to open a text file then print hello,but its not printing hello,instead its agin opening the imported one which again runs the GUI APP
I've edited to possibly fix your problem... Which GUI app runs?
but its not writing into file if used f.write('hello') instead of print even after providing f.close()

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.