Python reads files under the specified folder

Source: Internet
Author: User

1 #-*-coding:utf-8-*-2 ImportCSV3 ImportOS4 ImportPandas as PD5 #Extract folder under the address + file name, source file set collation6 deffile_name (file_dir):7L = []8      forRoot, dirs, filesinchOs.walk (file_dir):9          forFileinchFiles:Ten             ifOs.path.splitext (file) [1] = ='. csv': One L.append (Os.path.join (root, file)) A         returnL - Print(File_name ('c:/users/Charr/desktop'))

Returns all CSV files under the specified folder

Os.walk (FILE_DIR) returns parameter description: Returns a ternary group Dirpath, Dirnames, filenames, (folders and files are available under folder)

1 Import OS 2 Path = R'C:\Users\Administrator\Desktop\file'3for in  Os.listdir (path):4     print(Os.path.join (path,filename))

Os.listdir (PATH) returns parameter description: Returns a two-tuple dirpath, filenames, (only files in folder)

The value returned by the above two methods is the address + filename of the file, connected by join

Python reads files under the specified folder

Related Article

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.