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
#6
I think I understand what you want to do. Does this code produce a result to your liking?
# some named constants here so if you want to change these values, you don't need run around
# changing a bunch of hard-coded literals.
SOURCE_FILE_PATH = "C:\\Users\\John\Desktop\\StuffIn.txt"
DESTINATION_FILE_PATH = "C:\\Users\\John\Desktop\\StuffOut.txt"
SEARCH_TARGET = 'VisaRen'

# You want all the strings that contain your search target in your destination file, so we
# use a list and append everything to it.
matchedStrList = []
with open(SOURCE_FILE_PATH, "r") as f:
    # The readlines method returns a list with all the lines in the file, sans trailing 
    # newlines.
    for line in f.readlines():
        # list.append takes a single object as a parameter and puts it on the end of the list.
        if SEARCH_TARGET in line: matchedStrList.append(line)

with open(DESTINATION_FILE_PATH, "w") as f:
    # enumerate returns the tuple (index, value) where index is the index within the iterable
    # where the value appears. Using this syntax, we can unpack these two values into separate
    # variables
    for index, line in enumerate(matchedStrList):
        if index < len(matchedStrList) - 1:
            f.write(line + "\n")
        else:
            # Write the last item to the file without a trailing newline. (You can get rid of
            # this functionality if you want.)
            f.write(line)
I hope this solution helps. Python is for the most part, very well documented and there are a lot of tutorials available on many platforms. Don't be afraid to look around.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  very newbie problem on text file zapad 2 380 Apr-12-2024, 06:50 PM
Last Post: zapad
  Basic binary search algorithm - using a while loop Drone4four 1 514 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  Search Excel File with a list of values huzzug 4 1,415 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,582 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  search file by regex SamLiu 1 1,008 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  If function is false search next file mattbatt84 2 1,245 Sep-04-2022, 01:56 PM
Last Post: deanhystad
  Python newbie laleebee 2 1,418 May-24-2022, 01:39 PM
Last Post: laleebee
  fuzzywuzzy search string in text file marfer 9 4,840 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,232 May-31-2021, 07:00 AM
Last Post: bowlofred
  Newbie on Python syntax rud0lp20 6 3,085 Apr-21-2020, 04:26 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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