6 python string file-io list text-files
我正在尝试使用以下函数在Python中打开.txt文件.
def get_my_string():
"""Returns a string of the text"""
f = open("/home/Documents/text.txt", 'r')
string = str(f.read())
f.close()
return string
Run Code Online (Sandbox Code Playgroud)
我希望"string"是打开文件中的文本字符串.但是,在调用上面的函数后,"string"是一个空列表.
Ale*_*lds 10
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)
Run Code Online (Sandbox Code Playgroud)