这是我的代码:
# header.py
def add_header(filename):
header = '"""\nName of Project"""'
try:
f = open(filename, 'w')
except IOError:
print "Sorry could not open file, please check path"
else:
with f:
f.seek(0,0)
f.write(header)
print "Header added to", filename
if __name__ == "__main__":
filename = raw_input("Please provide path to file: ")
add_header(filename)
当我运行这个脚本(通过执行 python header.py)时,即使我提供了一个不存在的文件名,它也不会返回函数中的消息。即使我用 return 语句替换 print 语句,它也不返回任何内容。我将如何在函数中显示消息?