In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to read file names by python". Friends who are interested may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to read file names by python".
The operating environment of this tutorial: windows7 system, Python 3.9.1 Dell G3 computer.
1. The os.listdir () function only gets the file name under the current path, excluding the file in the subdirectory, and needs to get the file name recursively.
# returns all files under the specified path, excluding subdirectories. Path2 = r "C:\ Users\ 11764\ Desktop\ Data" f = os.listdir (path2) print (f) output: ['2020-09-16,' 2020-10-11, 'baidu_index_0625.xlsx',' city_id.xlsx', 'city_index_0625.xlsx',' province_id.xlsx', 'province_index_0625.xlsx']
2. The os.path.splitext () function splits the path into a file name and an extension (suffix).
The result of os.path.splitext (svm.model) [0] is that svm makes a decision on all the file names, and then adds them to the corresponding list.
Import os# saves directory names of different models (absolute path) file_dir = ritual C:\ Users\ zhangyh5\ Desktop\ xietong1012\ model'bayes_list = [] # Bayesian model list svm_list = [] # svm model list xgboost_list = [] # xgboost model list # root refers to the current directory path (the absolute path to the folder) # dirs refers to the subdirectories under the path ( The folder in the folder) # files refers to all the files under the path (all the files in the folder) for root Dirs,files in os.walk (file_dir): for file in files: if os.path.splitext (file) [0] = 'bayes': bayes_list.append (os.path.join (root,file)) elif os.path.splitext (file) [0] =' svm': svm_list.append (os.path.join (root) File)) elif os.path.splitext (file) [0] = 'xgboost': xgboost_list.append (os.path.join (root,file)) so far I believe you have a deeper understanding of "how to read file names from python", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.