Apr-18-2019, 09:26 PM
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.