Python Forum
I cannot able open a file in python ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I cannot able open a file in python ?
#1
Myself is ted,
i am trying open a text file in python using open("filename.txt") but i am seeing FileNotFoundError: [Errno 2] No such file or directory: 'new.text' this error, every time i tired to open or manipulate a file i am seeing this error.The name of file i inputted is right and i also created python file and text file In a folder and i tried to open using the above syntax still showing the same error , can anyone give me the solution ?


this is the code i am using to open a file in python:
h = open("new.text")
x = h.split()
print(x)

Attached Files

.py   1.py (Size: 47 bytes / Downloads: 116)
Reply
#2
(Feb-10-2023, 09:39 PM)ted Wrote: o such file or directory: 'new.text'
new.text should not be there,as the file extension is .txt.
Also split() will not work on file object.
h = open("new.txt")
x = h.read().split()
print(x)
Output:
['Hello', 'world']
Bettter.
with open("new.txt") as fp:
    result = fp.read().split()
    print(result)
Output:
['Hello', 'world']
Reply
#3
One can also fetch the whole content of the file with a higher level library
import pathlib
print(pathlib.Path('new.text').read_text().split())
Reply
#4
i am seeing this error:Traceback (most recent call last):
File "/home/teddy/Videos/Python/python/2.py", line 2, in <module>
print(pathlib.Path('new.text').read_text().split())
File "/usr/lib/python3.10/pathlib.py", line 1134, in read_text
with self.open(mode='r', encoding=encoding, errors=errors) as f:
File "/usr/lib/python3.10/pathlib.py", line 1119, in open
return self._accessor.open(self, mode, buffering, encoding, errors,
FileNotFoundError: [Errno 2] No such file or directory: 'new.text'

Attached Files

Thumbnail(s)
   
Reply
#5
(Feb-10-2023, 11:25 PM)ted Wrote: i am seeing this error
The name of the file is probably new.txt instead of new.text. Also it could happen that the file is not in the current directory.

By default the path is relative to the current working directory of the running python process. You could pass the absolute path to the file, for example
(pathlib.Path.home()/'spam'/'eggs'/'new.txt').read_text()
Reply
#6
i solved the problem thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 338 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,160 Dec-14-2023, 08:03 AM
Last Post: shanoger
  How can i combine these two functions so i only open the file once? cubangt 4 876 Aug-14-2023, 05:04 PM
Last Post: snippsat
  testing an open file Skaperen 7 1,393 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,413 May-06-2022, 01:44 PM
Last Post: Larz60+
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,061 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Open an excel file Newbie1114 1 2,352 Jun-16-2021, 09:11 PM
Last Post: Gribouillis
  How to open MIDI-file and get events in a list? philipbergwerf 7 5,026 May-29-2021, 08:24 AM
Last Post: j.crater
  Error on open of file created with tempfile.TemporaryDirectory() Brian177 4 6,310 Apr-05-2021, 07:12 PM
Last Post: Brian177
  I can't open a python file :( Oshadha 2 3,006 Mar-28-2021, 11:00 PM
Last Post: pythonprogrammer1101935

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020