Python3. Iterating through a folder extracting an instance of a specific file name

Source: Internet
Author: User
Below for everyone to share an article Python3. Traversing a folder to extract a specific file name of the instance, has a good reference value, we hope to help. Come and see it together.

When working with files in bulk, it is often necessary to traverse a path to extract the file name of a particular condition. This is a very concise and simple way to write a violent traverse, but it's very simple but very violent.

The objective of this example is to obtain the folder contents with the folder name ending with "_bad" in the folder where the remote sensing data is stored. Because the file is graded a lot (year/month/product Type/), the target folder is many and exists at the last level, manual viewing is annoying.

The code is as follows (after the Knowledge Point summary code):

#-*-Coding:utf-8-*-"" "Traverse all folders under a path, get all the files under a specific folder very violent, really traverse all the folders 20180124@author: Ink" "" Import Ostargetpath = R ' f:\ Modis_data ' records = []for Currentdir, _, Includedfiles in Os.walk (TargetPath): If not currentdir.endswith (' _bad '): Conti Nue Else:  records.append (currentdir) # Add the folder name ending with "_bad" to records  Records.extend (includedfiles) # Extend the list of file names in this folder to records# to write records. Txttxtfile = Open (Os.path.join (TargetPath, ' 02_04_bad.txt '), ' W ') Txtfile.write ( Os.linesep.join (Records)) Txtfile.close () # writes the sorted records. Txtwith Open (Os.path.join (TargetPath, ' 02_04_bad_ SORTED.txt '), ' W ') as TxtFile:txtFile.write (' \ n '. Join (SORTED (records)))

Os.walk () returns to the directory tree generator. Each time a tuple is generated in the format (Dirpath, Dirnames, filenames), the element is in turn the current path, the folder list under the current path, and the file name under the current path.

The list's. Append (),. Extend (), and. Sort () methods are all modified in situ, and the sorted () function is not.

When you write a list to a. txt file, you need to turn the list to STR, which is very ugly with the STR () function, and it looks a lot better to connect the list with newline characters.

Os.path represents the system line break, Windows is "\ r \ n", and many other systems are "\ n". However, whether you connect the list element with Os.path or "\ n", and finally use Windows Notepad to open the line, it will be wrapped, but with VS Code open Words Os.path will be more than one line, which seems to be a row between lines, which is why it is a bit to say, it may be related to the write mechanism of Python, temporarily do not delve into the (pit).

With regard to file reading and writing, most of the data is recommended with as form, which is indeed a little more concise.

Ps:

Said Os.walk () violence is because it really follow the directory tree to traverse all the folders and files in the given path, the file size and the number of filenames to be looked for is slower (in fact, I think not much slower), with Os.listdir () written as a recursive function, the execution efficiency may be higher, But Os.walk () logic is simple and good to write, everyone at random, I did!

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.