Python gets all the file names under the current folder

Source: Internet
Author: User

Https://www.cnblogs.com/strongYaYa/p/7200357.html

There are two functions under the OS module:

Os.walk ()

Os.listdir ()

1     #-*-coding:utf-8-*-   2       3     import os  4       5     def file_name (file_dir):   6         for Root, dirs, files in Os.walk (file_dir):  7             print (root) #当前目录路径  8             print (dirs) #当前路径下所有子目录  9             Print (Files) #当前路径下所有非目录子文件  
1     #-*-coding:utf-8-*-    2        3     import os   4        5     def file_name (file_dir):    6         l=[]    7         for Root, dirs, files in Os.walk (file_dir):   8 for             file in Files:   9                 if Os.path.splitext (file) [1] = = '. jpeg '  :                     l.append (Os.path.join (root, file))         return L  12 13 14 # where the Os.path.splitext () function splits the path into file name + extension
1     #-*-coding:utf-8-*-   2     import os   3        4     def listdir (Path, list_name):  #传入存储的list 5         for file in Os.listdir (path):   6             file_path = os.path.join (path, file)   7             if Os.path.isdir ( File_path):   8                 listdir (File_path, List_name)   9             else:                 list_name.append (file_path )  

Python gets all the file names under the current folder

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.