open函数不返回文件描述符

时间:2014-03-17 03:12:55

标签: python

Python open函数不返回文件描述符。为什么会这样?我正在使用python 3.3。

test_file_open.py

#!/usr/bin/env python

import sys
if __name__ == '__main__':
    html = ""
    hmtl_fd = open("ns_temp.html",'r')
    for line in html_fd:
        html += line

输出:

Traceback (most recent call last):
  File "./test_file_open.py", line 7, in <module>
    for line in html_fd:
NameError: name 'html_fd' is not defined

权限:

-rwxr-xr-x 1 michael michael 160 Mar 16 23:03 test_file_open.py

2 个答案:

答案 0 :(得分:1)

变量名称中存在拼写错误,请参阅hmtl_fdhtml_fd

替换:

hmtl_fd = open("ns_temp.html",'r')

使用:

html_fd = open("ns_temp.html",'r')

答案 1 :(得分:1)

错字:hmtl_fd代替html_fd