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

Source: Internet
Author: User

We will inevitably use a lot of repetitive tedious work in software development, such as bulk modification of the interface strings in the code, batch download files and re-named by the rules, these work is particularly tiring, especially for me lazy people.

Repetition is one of the most unacceptable things for a good programmer, so we invent tools to automate repetitive work. I used to do this with a Linux shell script, but due to the limitations of shell scripting syntax, some of the complex functions can not be effectively and quickly implemented, and the written code is difficult to maintain, basically belong to one-time code, difficult to reuse. So after slowly beginning to try to use Python to complete some automation scripts, because of the Python language powerful expressiveness and strict writing specifications, these shortcomings are solved!

Recently, we are going to put together some of the daily development of the Python script for the open source, so there is this topic, hoping to help you get rid of the 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 deletes the specified string in a file in a bulk delete directory, and 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 directory a line with a specified string in a file that specifies the extension can specify a tool script that is temporarily written by directory and file extension, regardless of the app build 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 string as 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:             # Skip if containing string, otherwise write temporary file       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 El If op = = "-E": # gets the 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 (Pat H, find_str, extension) Sys.exit () elif op = = "-H": Print ("Usage:python delstr_path.py-p Y Our_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

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.