Python Forum
I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str
#1
I have a folder with .txt files. In that folder I want to list all the files and read each files and replace a specific String. The problem I get is that the error message is telling me there is no such file or directory which are strange since I can list the files with a print. The "test2.txt" that are listed in my error message is just one of the files in that specific folder.


This is my code:
import os

#declare my folder
ws_folder =r"C:\FMEData\test"

#declare what to replace
search_text = "FMEData2019"
replace_text = "FMEData"

#list files in directory
listdir=os.listdir(ws_folder)
#printing files in directory for check
print (listdir)

for file in listdir:
    #checking if i can print the files
    print(file)
    
    #Open each file as read mode
    openfile= open(file,"rt")
    
    #read each opened file
    data=openfile.read()
    
    #replace text for each opened file
    data = data.replace('search_text', 'replace_text')
    #close the file
    openfile.close()
    
    #the open file as write mode
    openfile = open("file", "wt")
    openfile.write(data)
    openfile.close()
And this is my error message:
Error:
openfile= open(files,"rt") FileNotFoundError: [Errno 2] No such file or directory: 'test2.txt'
I hope this question is correcly made since I am new to booth python and this forum.

What I would like to achive is to understand why I got this error message. Dont get it. And if possible get some feedback of the code if I have the correct approach.
Reply
#2
does 'text.txt' actually exist?
using pathlib, you can narrow down the files you are interested in.
example list only .py files in src directory:
>>> from pathlib import Path
>>> path = Path('./src')
>>> files = [x for x in path.iterdir() if x.is_file() and x.suffix == '.py']
>>> for file in files:
...     print(file.name)
... 
AddressCorrection.py
BadgeDbModel.py
BadgePaths.py
Common.py
CreateDict.py
DecypherCaptcha.py
DecypherCaptcha1.py
DetermineFileAge.py
GetPage.py
__init__.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 327 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,122 Dec-14-2023, 08:03 AM
Last Post: shanoger
  I am getting a valueError. And not sure why? My goal is to visualize the correlation ReadytoCode 0 472 Dec-11-2023, 05:33 AM
Last Post: ReadytoCode
  Goal Seek tdutcher05 1 782 Nov-17-2023, 10:33 PM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 3,368 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 763 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can i combine these two functions so i only open the file once? cubangt 4 865 Aug-14-2023, 05:04 PM
Last Post: snippsat
  I cannot able open a file in python ? ted 5 3,341 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,384 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  Replace columns indexes reading a XSLX file Larry1888 2 989 Nov-18-2022, 10:16 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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