The bulk deletion of Brother Tao's Python toolbox with a specified string line

Source: Internet
Author: User

In software development, We inevitably use a lot of repetitive tedious work, such as the bulk of the code to change the interface strings. Download files in bulk and name them again by Rule. These jobs are particularly tiring, especially for lazy people like Me.

For an excellent program ape, repetition is one of the most unacceptable things, so we have to invent tools to make our work Self-active. I used to use Linux shell scripts to complete these things, but because of the limitations of Shell scripting Syntax. Some complex functions can not be effectively implemented at high speed, and the written code is difficult to maintain, basically belongs to one-time code, very difficult to reuse. So slowly start trying to use Python to complete some of your own proactive scripting. Because of the strong expressiveness and strict writing standards of the Python language, these shortcomings have been overcome!

In the near future, some of the Python scripts used in the daily research and development process have been gradually compiled to open source. So there is this topic, hope can help you get rid of those tedious work.

The code is implemented in the most pythonic way, the tool code is constantly updated, the project git address https://github.com/offbye/TaoPyTools, Remember to the star AH

today, we will analyze a script that contains the rows of a specified string in a file with the specified extension in a bulk delete folder. This script can be used to delete the log statements in the code in BULK.


#!/usr/bin/python2.7#-*-encoding:utf-8-*-# Copyright Offbye "" "Delete lines contains some string form all expected Extension files in Path bulk Delete folder Specifies the extension of the file with the specified string to specify a tool script to temporarily write the folder and file extension, regardless of the app generation system Usage:python delstr_path.py-p YOUR _path-e file_extension-s contain_string_want_to_delete "" "__author__ = ['" Xitao ":<[email protected]> ']    Import sysimport osimport shutilimport getoptdef delstr_path (p, findstr, suffix= ' js '): # Pass path and two strings as a parameter Workdir = P Os.chdir (workdir) CWD = os.getcwd () dirs = Os.listdir (cwd) for tmp in Dirs:path = Os.path.join (cwd, TM P) #print ' path= ', path #假设是文件 if Os.path.isfile (path): #推断文件扩展名 if OS.PATH.SPL                Itext (tmp) [1][1:] = = Suffix:tmp_name = path + '. bak ' tmp_file = open (tmp_name, "w")                    With open (path) as F:lines = F.readlines () for line in Lines:           # If the include string is skipped, otherwise a temporary file is written         If Line.find (findstr) > -1:continue tmp_file.write (line)        Tmp_file.close () # Replace the original file with the new file Shutil.move (tmp_name, Path) #假设是路径, Recursive Elif os.path.isdir (path): Print ("Enter dir:" + path) delstr_path (path, findstr) If __name__ =    = "__main__": Print ("delele contains str in Path") opts, args = getopt.getopt (sys.argv[1:], "hp:e:s:") Path = "        find_str = ' extension = ' for op, value in opts:if op = = "-p": # get Path = value Elif op = = "-e": # get path Extension = value elif op = = "-s": Find_str = value Print ("Delete lines contains {0} form all {1} files in path {2}". format (find_str, extension, path)) delstr_path (p  ath, find_str, extension) sys.exit () elif op = = "-h": Print ("usage:python delstr_path.py-p Your_path-e File_exteNsion-s contain_string_want_to_delete ") sys.exit () 


The bulk deletion of Brother Tao's Python toolbox with a specified string line

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.