Answers:
此处的键是代表打开文件name的f对象的属性。你得到这样的:
>>> f = open('/Users/Desktop/febROSTER2012.xls')
>>> f.name
'/Users/Desktop/febROSTER2012.xls'
有帮助吗?
tempfile模块文件的文档,尤其是有关tempfile.NamedTemporaryFile的文档,位于tempfile.TemporaryFile您所提到的文档的正下方。这是临时文件的特定情况,如文档所示,已经存在解决方案。tempfile.TemporaryFile并非要在您要读取名称的情况下使用。
我有完全相同的问题。如果使用相对路径,则os.path.dirname(path)将仅返回相对路径。os.path.realpath可以解决这个问题:
>>> import os
>>> f = open('file.txt')
>>> os.path.realpath(f.name)
os.path.realpath将返回“〜/ text.txt”而不是“〜/ Documents / text.txt”。
而且,如果您只想获取目录名称,而无需使用它附带的文件名,则可以使用osPython模块按照以下常规方式进行操作。
>>> import os
>>> f = open('/Users/Desktop/febROSTER2012.xls')
>>> os.path.dirname(f.name)
>>> '/Users/Desktop/'
这样,您就可以掌握目录结构。
f = open('febROSTER2012.xls')。您怎么能走完整的路?
tempfile.TemporaryFile(mode='w', prefix='xxx', suffix='.txt')不起作用!