简体   繁体   中英

Copy lines from one file to another

I trying to create a script that will allow me to designate an input file and take lines from that file and copy them into a separetly designated destination file. So far I have been able to get pretty close to my goal but the issues I am running into and that I cannot seem to find an answer to (that I can understand atleast) is that when I do my search on my string of data if the word "not" or "is" or any other scripting term is in my string it acts like it, meaning if I search on something like "This is not want I want" it takes the not operator into action and wont copy anything. So I guess my question is what can I do to stop this from happening (I've added my script to the bottom, be kind its my first attempt =)

My second question maybe asking a little to much out of the program but I'll ask it anyway. Much like in the above say I have a txt file with 1000 lines of stuff (individual lines not like a paragraph) and 250 of them start with "This is the line I want to copy" but then each say an individual reason why (This is the line I want to copy cause its awesome, cause its purple. etc. etc) as it sits now my script will just pull the first "This is the part I want to copy" and leave it at that, is there a to have it grab the rest of the string from all of the lines?

Again this might be an easy thing but since I am attempting some self education though trial and error some of the terminology I run across in troubleshooting is just as confusing as the problems I am having =)

#This will let you search for a string in a file and transfer it to another file
#It will search for texts or numbers

print "This is a tool to search one file and transfer information to another."
print
#select source file
a=open(raw_input("Enter full path of source file using using \\ instead of \: "), "r")
#select destination file
b=open(raw_input("Enter full path of destination file using \\ instead of \: "), "a")       
a
b
#actual copy command
copy=raw_input("What are you searching for: ")
for line in a.readlines():
    if copy in line:
        b.write(copy + '\n')
        yes='y'
        for again in raw_input("Would you like to search for another line? y/n: "):
            if again in yes:        
                copy=raw_input("What are you searching for: ")
                if copy in line:
                    b.write(copy + '\n')
    b.close
    a.close

if I search on something like "This is not want I want" it takes the not operator into action and wont copy anything.

That's impossible. In fact, I just tried, and it does no such thing. There must be something else you're doing wrong.

is there a to have it grab the rest of the string from all of the lines?

Replace

b.write(copy + '\n')

with

b.write(line)

Did you just switch from using input to raw_input ? The problems you are describing (also the fact that you think you need to double backslashes in the path) would arise from using input() under python 2 to read from the terminal. raw_input should not have such a problem.

Your second problem is clear: Don't do b.write(copy + '\\n') , do

b.write(line)

( line already contains a newline, as @lars just pointed out)

Perhaps using python's re module would be more helpful here. For example:

#All your existing code to retrieve input and output file names, etc.
#Convert the user's search string into a raw string
copy = eval("r'%s'" % (raw_input("What are you searching for?: "),))

import re

searchRegEx = re.compile(copy)

for line in a:
  matched = searchRegEx.search(line)

 if matched != None:
    b.write(line)

Secondly, instead of having the nested loop asking if the user wants to search for another line (while you're already searching for one) it might be more prudent to wrap the entire "Get search string input and do search logic" into a function and call it repeatedly while the user still wants to continue searching (ie using a boolean to control flow to it that's set to True as long as the user inputs a 'y' when you ask them).

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM