从 Python 中打开的文件中获取路径 [英] Get path from open file in Python

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

问题描述

如果我有一个打开的文件,是否有一个 os 调用来获取完整路径作为字符串?

f = open('/Users/Desktop/febROSTER2012.xls')

f,我将如何获得 "/Users/Desktop/febROSTER2012.xls" ?

解决方案

这里的关键是代表打开文件的 f 对象的 name 属性.你是这样理解的:

<预><代码>>>>f = open('/Users/Desktop/febROSTER2012.xls')>>>f.name'/用户/桌面/febROSTER2012.xls'

有帮助吗?

If I have an opened file, is there an os call to get the complete path as a string?

f = open('/Users/Desktop/febROSTER2012.xls')

From f, how would I get "/Users/Desktop/febROSTER2012.xls" ?

解决方案

The key here is the name attribute of the f object representing the opened file. You get it like that:

>>> f = open('/Users/Desktop/febROSTER2012.xls')
>>> f.name
'/Users/Desktop/febROSTER2012.xls'

Does it help?

这篇关于从 Python 中打开的文件中获取路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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