Python tkFileDialog.asksaveasfile - 获取文件路径

问题描述 投票:0回答:5

我想获取文件“exportFile”的路径。

exportFile = tkFileDialog.asksaveasfile(mode='a')

如果我写“print exportFile”,我得到:

<open file u'C:/Users/Desktop/Test/aaaa.txt', mode 'a' at 0x02CB6078>

但我只需要路径 - “C:/Users/Desktop/Test/aaaa.txt”。 有什么解决办法吗?谢谢。

python python-2.7 tkinter savefiledialog
5个回答
5
投票

试试这个:

exportFile = tkFileDialog.asksaveasfile(mode='a')
exportFile.name

它会返回:

'C:/Users/Desktop/Test/aaaa.txt'

3
投票

使用

tkFileDialog.asksaveasfilename
代替
tkFileDialog.asksaveasfile
.

NOTE

tkFileDialog.asksaveasfilename
不采用
mode
参数。


0
投票

尝试

tkFileDialog.askdirectory
而不是任何文件名对话框。这将返回目录而不是文件名。


0
投票

尝试打印“exportFile.name”而不是打印“exportFile”。它应该给你想要的输出


0
投票

谢谢,Jan Wilmar.

有人可以对此进行扩展吗?如果没有文件名,您将如何获得路径位置。 'C:/用户/桌面/测试' 或者 如果你只需要文件名 'aaaa.txt'

假设您改为打开一个文件: FileLoc=filedialog.askopenfilename()

我知道你可以做这样的事情来获得想要的结果: 导入操作系统 base=os.path.basename(文件位置) path=os.path.dirname(文件位置)

© www.soinside.com 2019 - 2024. All rights reserved.