我是殼牌在Python成功地做這個Python的錯誤:讀取文件中的內容到變量
readonly FILE="/home/xxxx/file.txt"
text=$(cat ${FILE})
試圖同
text = open('file.txt').read()
print text
我想這一點,得到了一個錯誤:
Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\lib\py_compile.py", line 170, in <module>
sys.exit(main())
File "C:\Python27\lib\py_compile.py", line 162, in main
compile(filename, doraise=True)
File "C:\Python27\lib\py_compile.py", line 106, in compile
with open(file, 'U') as f:
IOError: [Errno 22] invalid mode ('U') or filename: ''
謝謝!
您放置的代碼似乎與您發佈的追溯相匹配。在trackeback中,您似乎正在使用上下文管理器。你能否澄清這一點? – SH7890
'text = open('file.txt','r')。read()' –