0 foreword
About three months ago, someone came to me and asked me to write a few lines of code, and the function was to split all the files in one folder into 200 files in one folder, very simple.
After I understood it, I thought it was very simple. It took me half an hour to write it. There was not much code.
1 Environment Description
Win10 Python3, the compiler is Pycharm.
To install shutil, you can simply PIP install shutil.
2 code
Enter the libraries you need. The OS is used to switch paths and create folders.
Shutil is used to copy and paste files
import os
from shutil import copy
Copy the code
I is used to count the number of files. If I is a multiple of 200, k +1. K is used to calculate the number of new folders to facilitate folder creation.
Save_dir is the path where you want to save the copied file
# I is used to count the number of files and k is used to count the number of new folders
i = 0
k = 0
The root path to which you want to save
save_dir = r'F:\666'
If the directory does not exist, create it
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
Copy the code
Dir_name is the name of the new folder; File_path is the path of the folder you want to split, that is, the path of a large number of files.
Os. listdir(file_path) gets the list of files or folders contained in the specified path, used on Unix and Windows.
# Want to save the name
dir_name = "junzi"
The path of the folder you want to split
file_path = r'F:\ALL'
Get the list of files and folders under file_path
# file_path does not contain folders
pathDir = os.listdir(file_path)
Copy the code
Once you’ve got the list, go through it one by one. 0 is a multiple of 200, so k is going to be plus 1 at the beginning.
AllDir is the file name + suffix. From_path is the full path of the currently traversed file, and to_path is the path to which the file is copied.
Then copy(from_path, to_path) completes the copy paste, and finally I increases by 1.
for allDir in pathDir:
if( (i%200) = =0):
print("Multiples of 200, create a new folder.")
k += 1
print(allDir)
from_path = os.path.join(file_path, allDir)
to_path = save_dir + "\ \" + dir_name + str(k)
# if the to_path directory does not exist, create it
if not os.path.isdir(to_path):
os.makedirs(to_path)
copy(from_path, to_path)
i += 1
Copy the code
3 Related Description
Shutil’s copy() is copied to a new location. The create time, modify time, and access time are all new. Copy2 () is copied to the create time, modify time, and access time.
For this simple procedure, I would like to mention 88.88. Maybe this made me a little lost. A few days later, someone asked me to do outsourcing. I understood the requirements of a simple small program and thought it was simple and could be completed in a week.
It was really simple at the beginning, but later the requirements were changed. Then I delayed a lot of practice because of school affairs, and Party A took some time to prepare materials.
That is to say, the outsourcing has not yet concluded 😂😂
Photo: Freestocks.org on Unsplash