简体   繁体   中英

Reading File function in python

hello I am trying to apply the file reading function in python

f = open("C:\\Users\\hamza\\Desktop\\family.txt","r")
print(f.read())

It keeps giving me the error of

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    f = open("C:\\Users\\hamza\\Desktop\\family.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\hamza\\Desktop\\family.txt'

please help me to resolve this issue

Thankyou

That is the correct code to read a file. C:\\Users\\hamza\\Desktop\\family.txt" must not exist.

  1. You can try following code also,

     path="C:\\Users\\hamza\\Desktop\\family.txt" f = open(path,"r") print(f.read())

    But your code is also correct.

  2. Please check if file is present on desktop and name of file is correct or not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM