- I'm a lazy and studious person. I'm too lazy to pick up anything and can't put it down. In fact, it's quite hard. When I was a child, I could not see it or solve it when I saw it. But will I take the initiative to find a topic to do? Not because I'm too lazy to look for it. So later, in our words, "one bottle is dissatisfied, and half of the bottle is dangling.". In 2009 to participate in the national high school mathematics league, because of 3 points, and lost the first prize, has always been a pain in my heart. Fortunately, fate is not bad for me. Although I am busy now, I have been working with capable and kind-hearted people, which is also considered satisfactory. There are a lot of feelings. Let's get to the point~
Because of the epidemic, we once again return to the online learning mode. After the examination, some documents (answer cards) can not be sent to students in the form of paper version, so electronic version is adopted. However, the electronic version is based on the school, putting the same discipline of the whole grade under the same folder, and the list is also disordered. We need to classify the whole document by class, and then send it to the students by the subject teacher of the corresponding class.
- So what is today's so-called "big project"? To divide the file into different classes:
First of all, look at the source folder. In this way, there are hundreds of people in the grade, each of whom has four files:
The steps are as follows:
- For example, 10 classes can create 10 sheet tables in one excel. Each sheet is the student list of the class (for later screening);
- After the establishment of the class, we can carry out the screening:
- Preparation 1: find the place to search and the storage address of the finished destination
import os import shutil import xlrd src_dir_path = '/Users/carla/Desktop/2020 Online learning materials/achievement/Answer sheet(mathematics)' # Source folder to_dir_path = '/Users/carla/Desktop/4 Class answer cards' # The folder where the copied files are stored
- Preparation 2: reading tables with keywords with xlrd
path='/Users/carla/Desktop/4 Class list.xlsx' # The list is the key word wb = xlrd.open_workbook(path) sh = wb.sheet_by_name("Sheet1") nrows = sh.nrows # Get the number of rows print(nrows) # Number of output lines
- Introduce for loop:
t = () # Create an empty tuple for i in range(nrows): t = t+tuple(sh.row_values(i)) print(t) for key in t: if not os.path.exists(to_dir_path): print("to_dir_path not exist,so create the dir") os.mkdir(to_dir_path, 1) if os.path.exists(src_dir_path): print("src_dir_path exitst") for file in os.listdir(src_dir_path): # is file if os.path.isfile(src_dir_path + '/' + file): if key in file: print('Find contains"' + key + '"Character file,The absolute path is----->' + src_dir_path + '/' + file) print('Copy to----->' + to_dir_path + file) shutil.copy(src_dir_path + '/' + file, to_dir_path + '/' + file)
Note: why use tuples instead of lists? Because if you use the list list, you will form a small list when you call the names in excel. However, when searching for keywords, the object must be in string format, otherwise the search will fail. As for why, I don't quite understand. In a word, I tried it out.
- Of course, if you don't want to recreate a tuple, you can call the cell content directly* sh.cell_value(i,0) * for filtering, of course, the premise must be defined first
*sh = wb.sheet_by_name("Sheet1") * see below 👇👇 :
for i in range(nrows): key = sh.cell_value(i,0) if not os.path.exists(to_dir_path): print("to_dir_path not exist,so create the dir") os.mkdir(to_dir_path, 1) if os.path.exists(src_dir_path): print("src_dir_path exitst") for file in os.listdir(src_dir_path): # is file if os.path.isfile(src_dir_path + '/' + file): if key in file: print('Find contains"' + key + '"Character file,The absolute path is----->' + src_dir_path + '/' + file) print('Copy to----->' + to_dir_path + file) shutil.copy(src_dir_path + '/' + file, to_dir_path + '/' + file)
In this way, we in a large number of documents, with our class list of children to "pull out", put in the designated folder!
Pychar runs as follows:
(in order to protect the privacy of our dolls, it's really a waste of effort. I don't even have my beautiful photos taken with me! 😌😌)
- Every time a keyword is successfully searched, print ("SRC_ dir_ Path exits "), and then the address of the specific file is displayed and copied to the specified destination folder.
- Finally, I haven't learned online for many days. I'm either lazy (or watching TV Series), or I'm very busy. Summer vacation will certainly make up for my homework! Make a military order 🚩🚩.