|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#想改变下路径打开一个其他路径下的文件,结果打不开,报错,亲们帮忙看下
import sys
sys.path.append("F:\\自己")
class FileMgr:
def __init__(self,filename):
self.filename = filename
self.f = None
def __enter__(self):
self.f = open(self.filename,encoding='utf-8')
return self.f
def __exit__(self,t,v,tb):
if self.f:
self.f.close()
if __name__ == '__main__':
with FileMgr('1120.txt') as f:
for line in f.readlines():
print(line,end='')
错误提示:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "E:\py\PyCharm Community Edition 2019.1.3\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "E:\py\PyCharm Community Edition 2019.1.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "E:/py/21天/21天学通Python-源代码/C10/a10_5.py", line 18, in <module>
with FileMgr('1120.txt') as f:
File "E:/py/21天/21天学通Python-源代码/C10/a10_5.py", line 10, in __enter__
self.f = open(self.filename,encoding='utf-8')
FileNotFoundError: [Errno 2] No such file or directory: '1120.txt'
|
|