读取文件内容时No such file or directory: 'star.txt'
版本: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 “<pyshell#76>”, line 1, in
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.