def get_my_string():
"""Returns the file inputFn"""
inputFn = "/home/Documents/text.txt"
try:
with open(inputFn) as inputFileHandle:
return inputFileHandle.read()
except IOError:
sys.stderr.write( "[myScript] - Error: Could not open %s\n" % (inputFn) )
sys.exit(-1)
只要python文件和文本文件保存在同一个文件夹中,就不需要home/documents部分,只需要使用open()函数和字符串中的文本文件名来打开文本文件,这样就可以打开(“text.txt”),您可能不需要方括号,但可以使用它们。
相关问题 更多 >
编程相关推荐