我正在阅读一本关于基本Python的书,并且需要翻译此程序的帮助。 这将是我的第一个程序,所以请不要嘲笑我。解释基本的Python程序
我需要一步一步的指导,当我点击打印,它说fname not found
。
#!/usr/bin/env python
# 'makeTextFile.py -- create text file'
import os
ls = os.linesep
# get fileneame
while True:
if os.path.exists(fname):
print "ERROR: '%s' already exists" % fname
else:
break
# get file content (text) lines
all = []
print "\nEnter lines (' . ' by itself to quit). \n"
# loop until user terminates input
while True:
entry = raw_input('> ')
if entry == '.':
break
else:
all.append(entry)
# write lines to file with proper line-ending
fobj = open(fname, 'w')
fobj.writelines(['%s%s' % (x, ls) for x in all])
fobj.close()
print 'DONE'
如果这是您的第一个程序,那么您有点雄心勃勃。在尝试执行文件操作(这是更复杂的输入/输出形式)之前,您应该更好地理解给定编程语言中的基本输入/输出。 – Edwin 2012-02-29 16:19:35