Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python search for files in batches based on file names

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how python searches files in batches according to file names. The quality of the article is high, so Xiaobian shares it with you for reference. I hope you have a certain understanding of relevant knowledge after reading this article.

In the scenario, 500 files were selected and placed in a new folder.

1. Preparation 1. Installing Python Environment

It may be wrong, and pip install these things can not be.

2 Prepare an Excel file

filename.xlsx Write the name of the file selected by the fortress, as shown below

Code # encoding: utf-8 import osimport numpy as npimport pandas as pdimport shutil file_path ='/Users/User/Desktop/Notes' #file path filename_path ='/Users/User/Desktop/filename.xlsx'#file list filelist=os.listdir(file_path) #Get the file name in the folder file_name=pd.read_excel(filename_path) #Read the desired file list file_name <$'count']=0 #Define a new column count for counting for file in filelist: m=file_name.shape[0] #Number of rows in the table olddir=os.path.join(file_path,file) #Each file path for i in range(m): if str(file_name <$'name '][i]) in file: #Find the corresponding file name F="/Users/user/Desktop/myfiles_filter/" #New folder name (built first) newdir=os.path.join(F,file) shutil.copy(olddir,newdir) #Copy to new folder file_name <$'count '][i]=file_name <$'count'][i]+1 #count print(file) #Print the file name, actually I want to see if it is running else: continue file_name.to_excel ('file_name_count.xlsx')#Save new file list

And then at the terminal

cd file location python3 main.py

Note that the path must be written correctly. Open the new folder and you will find that 50 files have been written. A new excel sheet-file_name_count.xlsx-will be created for you to see the export results.

Python's five characteristics are what Python's five characteristics are: 1. Easy to learn, when developing programs, focus on solving problems, rather than understanding the language itself. 2. Compared to other major languages such as C++ and Java, Python implements object-oriented programming in a very powerful and simple way. 3. Portability: Python programs run on a variety of platforms without modification. 4. Interpretative, Python programs do not need to be compiled into binary code, and can be run directly from source code. 5. Python is one of FLOSS(Free/Open Source Software).

About python how to batch search files according to file names to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report