Python Forum
Basic Python 3 Win 10 Search Txt to File Newbie
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Python 3 Win 10 Search Txt to File Newbie
#4
You could have one loop do both the reading and writing; this is probably better anyway if that's all you are doing.

    infile = open("C:\\Users\\John\Desktop\\StuffIn.txt", "r")
    outfile = open("C:\\Users\\John\Desktop\\StuffOut.txt", "w")
    for line in infile:
        if stringToMatch in line:
            #write it to the output file
            outfile.write(line)
    infile.close()
    outfile.close()
Reply


Messages In This Thread
RE: Basic Python 3 Win 10 Search Txt to File Newbie - by rlgoodman - Apr-18-2019, 09:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  very newbie problem on text file zapad 2 309 Apr-12-2024, 06:50 PM
Last Post: zapad
  Basic binary search algorithm - using a while loop Drone4four 1 438 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  Search Excel File with a list of values huzzug 4 1,315 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,468 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  search file by regex SamLiu 1 957 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  If function is false search next file mattbatt84 2 1,202 Sep-04-2022, 01:56 PM
Last Post: deanhystad
  Python newbie laleebee 2 1,384 May-24-2022, 01:39 PM
Last Post: laleebee
  fuzzywuzzy search string in text file marfer 9 4,723 Aug-03-2021, 02:41 AM
Last Post: deanhystad
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 2,192 May-31-2021, 07:00 AM
Last Post: bowlofred
  Basic python Natters10 3 3,140 Nov-29-2020, 07:04 AM
Last Post: Love2code

Forum Jump:

User Panel Messages

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