Python implements the method of merging txt in a directory into a large TXT file

Source: Internet
Author: User
Tags glob
This example describes the Python implementation of the method of merging txt into a large TXT file in the directory. Share to everyone for your reference. Specific as follows:

In the online a DotA hero Raiders, txt format, each hero a file, see pain, wrote a small thing, merged.

#coding =gbkimport osimport sysimport globdef dirtxttolargetxt (dir,outputfilename):  "read all txt files from dir directory, Write them into the outputfilename. "  #如果dir不是目录返回错误  if not Os.path.isdir (dir):    print" error passed in parameter%s is not a directory "%dir    Return False  #list all txt files in dir  outputFile = open (OutputFileName, "a") for  txtfile in Glob.glob (os.pat H.join (dir, "*.txt")):    print txtfile    inputfile = open (txtfile, "RB") for line in    Inputfile:      Outputfile.write (line)  return trueif __name__ = = "__main__":  If Len (SYS.ARGV) < 3:    print "usage:%s Dir outputfilename "%sys.argv[0"    sys.exit ()  dirtxttolargetxt (sys.argv[1],sys.argv[2])

Hopefully this article will help you with Python programming.

  • 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.