免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2782 | 回复: 2
打印 上一主题 下一主题

关于os.tmpnam() ,高手帮忙啊 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-30 21:02 |只看该作者 |倒序浏览
这个函数是用来创建一个存放临时文件的目路径吗,那么这个路径是什么?
还有个类似的函数os.tempnam([dir[, prefix]]) ,这个函数和上一个差不多,不过可以指定临时文件的目录dir,不过下面说了句:
On Unix, the environment variable TMPDIR overrides dir, while on Windows TMP is used.
好像是说不管是unix还是windows,这个dir都被覆盖掉,也就是说这个dir没用啊,我在windows下作了实验:
>>> os.tempnam('C:\\')
'C:\\DOCUME~1\\fxww\\LOCALS~1\\Temp\\3'
>>> os.tempnam('D:\\')
'C:\\DOCUME~1\\fxww\\LOCALS~1\\Temp\\4'
>>> os.tempnam('C:\\WINDOWS')
'C:\\DOCUME~1\\fxww\\LOCALS~1\\Temp\\5'
>>> os.tempnam()
'C:\\DOCUME~1\\fxww\\LOCALS~1\\Temp\\6'
上面那些3,4,5,6啊是什么意思啊,好像不是目录啊
忘高手给解释一下吧,谢谢

论坛徽章:
0
2 [报告]
发表于 2009-12-31 02:24 |只看该作者
os.tempnam([dir[, prefix]])

os.tmpnam()

正如你所说的,第一个是可以指定目录和前缀名,而第二个则不可以。两个都是返回唯一路径名为临时文件名。

---------------------------------------
>>> os.tempnam()
'/tmp/filekopFkU'
>>> os.tempnam('/opt')
'/opt/filebArs1R'
>>> os.tempnam('/opt','Test')
'/opt/Test398mvS'

---------------------------------------
>>> os.tmpnam()
'/tmp/filevXeKMU'

---------------------------------------
我没有在win下的测试环境,
你测试返回的 'C:\\DOCUME~1\\fxww\\LOCALS~1\\Temp\\5' 是返回的绝对路径名来的,4,5,6只是unique。
至于为什么没有返回你指定的C:\\ D:\\,文档是这样说的

Availability: Unix, Windows. This function probably shouldn’t be used on Windows, though: Microsoft’s implementation of tmpnam() always creates a name in the root directory of the current drive, and that’s generally a poor location for a temp file (depending on privileges, you may not even be able to open a file using this name).


最后文档提议使用tmpfile(),这两个方法有安全性问题。
Warning

Use of tempnam() is vulnerable to symlink attacks; consider using tmpfile() (section File Object Creation) instead.


我也是初学的,讲得有误的,还请大牛指教

论坛徽章:
0
3 [报告]
发表于 2009-12-31 19:04 |只看该作者
至于为什么没有返回你指定的C:\\ D:\\,文档是这样说的

Availability: Unix, Windows. This function probably shouldn’t be used on Windows, though: Microsoft’s implementation of tmpnam() always creates a name in the root directory of the current drive, and that’s generally a poor location for a temp file (depending on privileges, you may not even be able to open a file using this name).

谢谢解答,但是上面这段只是说os.tmpnam()不能在windows下运行,我试了,的确不行
至于为什么没有返回你指定的C:\\ D:\\,我在上面已经说了:On Unix, the environment variable TMPDIR overrides dir, while on Windows TMP is used
我只是比较困惑,如果按上面所说那么os.tempnam([dir[, prefix]]) 函数中的dir不就没用了,因为他都要被覆盖,除非你没设置TMPDIR变量,是这样吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP