Python obtains the file name in a specified folder.

Source: Internet
Author: User

Python obtains the file name in a specified folder.

This document uses the OS. walk () and OS. listdir () methods to obtain the file name in the specified folder.

I. OS. walk ()

The walk () function in the module OS can traverse all files in the folder.

os.walk(top, topdown=Ture, onerror=None, followlinks=False) 

This function can obtain a Trielement tupple (dirpath, dirnames, filenames ).

Parameter description:

  1. Dirpath: string, indicating the directory path;
  2. Dirnames: list, which contains the names of all subdirectories in the current dirpath path (excluding the directory path );
  3. Filenames: list, containing the names of all non-directory sub-files in the current dirpath path (excluding the directory path ).

Note: Neither dirnames nor filenames contains the path information. To complete the path, you can use OS. path. join (dirpath, dirnames)

The following code is provided;

#-*-Coding: UTF-8-*-import OS def file_name (file_dir): for root, dirs, files in OS. walk (file_dir): print (root) # current directory path print (dirs) # All subdirectories in the current path print (files) # all non-directory sub-files in the current path

When a specific type of file is required, the Code is as follows:

# -*- coding: utf-8 -*-   import os  def file_name(file_dir):    L=[]    for root, dirs, files in os.walk(file_dir):     for file in files:       if os.path.splitext(file)[1] == '.jpeg':         L.append(os.path.join(root, file))   return L 

Among them, OS. path. the splitext () function splits the path into file names + extensions, such as OS. path. splitext ("E:/lena.jpg") will get "E:/lena" + ". jpg ".

Ii. OS. listdir ()

The OS. listdir () function obtains only the file names in the current path, excluding the files in subdirectories. All files must be obtained using recursive methods.

Directly send the code, and the response will be '.jpeg 'file names:

# -*- coding: utf-8 -*- import os  def listdir(path, list_name):   for file in os.listdir(path):     file_path = os.path.join(path, file)     if os.path.isdir(file_path):       listdir(file_path, list_name)     elif os.path.splitext(file_path)[1]=='.jpeg':       list_name.append(file_path) 

Python beginners have made some low-level errors, which wastes a lot of time. First, make sure that the four spaces of the Code are indented. Note that the next line of press enter is not indented by four spaces; another point is that in the Command Prompt window (Windows), after the python interpreter runs the code for the first time, modify the source code. If the interpreter runs again, exit and re-enter the python interpreter, otherwise, the Code executed for the first time will be run.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.