简体   繁体   English

python获取文件名并在另一个脚本中打开

[英]python to get file name and open in another script

I have a one.py as: 我有一个one.py作为:

one.py 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': 我还有另一个文件two.py,需要从“ 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! 请帮我解决问题,它没有得到文件名。答案将不胜感激!

One.py: One.py:

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

Main.py: 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. 在Python中,除非返回变量,否则无法访问函数中的变量。

Edit: 编辑:

Try 尝试

f = open(file_save, 'w')
print('hello')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM