Python merges multiple text files into one text code (for easy searching) _python

Source: Internet
Author: User
But when a book has been learned, to the general technology and functions have an impression, suddenly want to find a function of the instance code, but feel very difficult, because a book's source code directory is very long, often have dozens of or even hundreds of source codes files, want to find the function instance you want is not easy?

So this is where all the source code is labeled as a directory and file name, all merged into one place, making it easy to quickly search. Find, no, then find the next ... So it is very convenient to find the example that you want very quickly. Of course, separate source code files are still useful and can be retained. After merging the source code file is not big, n*100kb just, open and search are very fast. You can combine all instances of the same programming language into one file in this way, and the search efficiency can be greatly improved.

Note: After you save the code, copy the source files to the directory, and all directories and subdirectories in the same directory are searched; You can add suffix limits, only get the contents of a file in a format; The source code is as follows, please copy and save:
Copy Code code as follows:

#-*-Coding:utf-8-*-

Import Os,sys
info = OS.GETCWD ()
Fout = open (' Note.tpy ', ' W ') # Merge content to the file

def writeintofile (info):
Fin = open (info)
Strinfo = Fin.read ()
# using the # #作为标签的点缀, you can also use the other
Fout.write (' \n##\n ')
Fout.write (' # # ' +info[-30:].encode (' utf-8 '))
Fout.write (' \n##\n\n ')
Fout.write (Strinfo)
Fin.close ()


For Root, dirs, the files in Os.walk (info):
If Len (dirs) ==0:
for FL in Files:
info = "%s\%s"% (ROOT,FL)
If info[-2:] = = ' py ': # merges only the contents of the file with the suffix named py
Writeintofile (Info)

Fout.close ()

If you do not want to merge content, just want to get a file name of the manifest file, also can. Here's the code for you. For example, some authors use this feature to generate a list of source code files for themselves, which is useful.

The source code is:
Copy Code code as follows:

#-*-coding:utf-8-*-
'
This program automatically searches for the specified directory,
prints the full file name of all files to the specified file
'
Import os,sys
Export = ""
I=1
for root, dirs, files in Os.walk (R ' ... '):
#r '. ' Represents all listings in the current directory,
#. Represents parallel to other directories, many more content
Export + + "--%s--\n%s\n\n%s\n\n"% (i,root, ' \ n '. Join (files))
i=i+1
fp = open (' cdcfile-4 . txt ', ' W ')
Fp.write (export)
Fp.close ()

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.