Preface:

Today we will use Python scripts to automatically categorize files. Directly open the whole ~

Preliminary knowledgeCopy the code

This script is relatively simple to implement, and I have listed the knowledge points involved.

1, relative path, absolute path, absolute path is the most complete path.

‘D:/code/gitpython.py’

Relative path is relative to the current folder path, which is the folder path that you’re writing this py file in.

‘gitpython. Py’ or ‘. / gitpython. P y ‘

2. OS module and Shutil module

os.listdir(path)

Path — The directory path to be listed

Path = "./" dirs = os.listdir(path) Print file # Run result: 1.docx 1.jpg 1.pptxCopy the code

Move a file (directory) shutil.move(“oldpos”,”newpos”)

The source code to show

Import OS import shutil path = "./" # py file = os.listdir(path) # list all files in the current folder # print(f) # folder_name = path + f.split(".")[-1] If not os.path.exists(folder_name): # shutil.move(f, folder_name) # move(f, folder_name) shutil.move(f, folder_name)Copy the code

That’s the end of this article, and thanks for watching. The next article in this series of useful Python scripts will share the classic Pac-Man games

To thank the readers, I’d like to share some of my recent programming gems to give back to each and every one of you.

Dry goods are mainly:

① More than 2000 Python ebooks (both mainstream and classic)

Python Standard Library (Chinese version)

(3) project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis videos (suitable for small white learning)

⑤Python Learning Roadmap

⑥ Two days of Python crawler boot camp live permissions

See profile or private message for complete source code.