且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-09-18 08:50:19

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')

在Python中,除非返回函数,否则无法访问函数中的变量。

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

编辑:

尝试

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