我无法在文件上读取和执行cat(文件名)函数小.txt在我的windows终端。python脚本名为你好,派瑞. 跑步你好,派瑞小.txt没有显示结果。脚本代码如下:
import sys
def cat(filename):
f=open (filename,'rU')
text = f.read()
print text
def main():
cat (sys.agrv[1])
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()
RESTART: C:\Users\WELCOME\google-python-exercises\hello.py
Traceback (most recent call last):
File "C:\Users\WELCOME\google-python-exercises\hello.py", line 34, in <module>
main()
File "C:\Users\WELCOME\google-python-exercises\hello.py", line 30, in main
cat (sys.agrv[1])
AttributeError: 'module' object has no attribute 'agrv'
有一个拼写错误
agrv
。读取文件后,不能关闭它。试试这个:我将使用
with
语句重写cat()
函数,因为它负责关闭文件。你知道吗Python Tutorial说:
相关问题 更多 >
编程相关推荐