Python determines whether a string in a file exists in another file

Source: Internet
Author: User
Tags python script

Recently, the marketing department's colleagues asked me to help deal with a two files, to determine whether the content of a file exists in the B file, if there is the content of the B file, think of the next, the shell is very simple implementation, with Python how to achieve it? Here is the specific code:


Shell code:

#!/bin/bashfor line in ' cat/root/zy/audiolist.txt ' do files= ' Cat/root/zy/list.txt|grep "$line" |wc-l ' if [-N $  Files] then echo ' cat/root/zy/list.txt|grep ' $line ' >> shell-script.txt else echo ' file is not exist "Fidone


Shell script output input directly into file Shell-script.txt


python code:

#!/usr/bin/env pythonfobj=open ('/root/zy/python-secipt.txt ', ' rw+ ' ) With open ('/root/zy/audiolist.txt ', ' R ')  as f:     for line  In f:          with open ('/root/zy/list.txt ', ' R ')  as obj:                   for strs in obj.readlines ():                          if  Line.strip ()  in strs.strip ():                                fobj.write (Strs.strip () + "\ n") Fobj.close () 


The Python script directly outputs the file to Python-secipt.txt

This article from "Flat Light is true" blog, please be sure to keep this source http://ucode.blog.51cto.com/10837891/1893441

Python determines whether a string in a file exists in another file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.