读取文件内容时no such file or directory: 'star.txt' | python | python 技术论坛-江南app体育官方入口
版本:window10 python3.12.4
需求:读取文件star.txt中内容
问题:显示filenotfounderror: [errno 2] no such file or directory: ‘star.txt’,已做步骤陈列如下,是哪里出了问题,烦指出
file_name = ‘star.txt’
with open (file_name) as file_object:
lines = file_object.readlines()
traceback (most recent call last):
file “
with open (file_name) as file_object:
filenotfounderror: [errno 2] no such file or directory: ‘star.txt’
补充:已确定拥有读取star.txt权限。
在下萌新,望前辈不吝赐教,谢谢!
check if current working directory,
os.getcwd()
, to confirm if it is same directory for yourstar.txt
.if not, you should add the path to the
star.txt
file to tell python where to find your file.