python获取文件名并在另一个脚本中打开 [英] python to get file name and open in another script

查看:301
本文介绍了python获取文件名并在另一个脚本中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个one.py:

I have a one.py as:

one.py

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

我有另一个文件two.py,我需要从'one.py'打开'filename':

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!

推荐答案

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

这篇关于python获取文件名并在另一个脚本中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆