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.