[python] Move files that meet the conditions

Thing you want to do

--Move files that meet the conditions -(Example) Set the condition to a file name that includes "today's date"

point

--File search pathlib --Move shutil

Sample code

import pathlib
import shutil
import datetime

#Today's date
yyyymmdd = datetime.date.today().strftime('%Y%m%d')
#Get the file name that matches the conditions from the source folder
#I want to receive it in the list, so cast it
p_tmp = list(pathlib.Path('Source folder').glob(f'{yyyymmdd}*.csv'))
'''Corrected the list to ↑
p_tmp = pathlib.Path('Source folder').glob(f'{yyyymmdd}*.csv')
p = [p for p in p_tmp]
''' 
dest = 'Destination folder'
#Move file
for source in p_tmp:
    shutil.move(str(source), dest) 

--The file name picked up by pthlib is returned by ** Generator ** -** Store in list ** and retrieve file names one by one with for statement -↑ Although it was listed in the comprehension notation, it turned out that you can cast with ** `list``` ** without having to bother to do that. --** Convert the extracted file name with str``` ** -** ``` Shutiful.move ([Move source], [Move destination]) Move file with `**

Recommended Posts

[python] Move files that meet the conditions
Delete vertices that meet the conditions in networkx
[Python] A program that calculates the number of chocolate segments that meet the conditions
Extract only elements that meet specific conditions in Python
[Python] Check the current directory, move the directory
Python3 + pyperclip that rewrites the copied text
Download files on the web with Python
[Python] A program that rounds the score
Extract lines that match the conditions from a text file with python
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame
[Python] Get the files in a folder with Python
How to get the files in the [Python] folder
The one that displays the progress bar in Python
Python script that makes UTF-8 files with all BOMs under the folder without BOMs
Extract rows that meet the conditions from Excel containing date data (% Y /% m /% d)
Extract the table of image files with OneDrive & Python
[Python] A program that counts the number of valleys
Find out the location of Python class definition files.
[Python] Evaluate the facial expressions that appear on the face
[Python] Generate random numbers that follow the Rayleigh distribution
Find the part that is 575 from Wikipedia in Python
Python program that looks for the same file name
PHP and Python samples that hit the ChatWork API
A memo that I touched the Datastore with python
I felt that I ported the Python code to C ++ 98.
Modules that may go through the shell in Python
The attitude that programmers should have (The Zen of Python)
[Python] A program that compares the positions of kangaroos.
the zen of Python
Check what the character code is for all files under the directory that is Python and output
[Python] Split the date
Python --Tagging MP3 files
[Python] Reading CSV files
Recursively copy files from the directory directly under the directory using Python
Creating a Python script that supports the e-Stat API (ver.2)
The story that `while queue` did not work in python
Consideration for Python decorators of the type that passes variables
[Part.2] Crawling with Python! Click the web page to move!
The story that Python stopped working with VS Code (Windows 10)
Miscellaneous notes that I tried using python for the matter
[Python] A program that finds the most common bird types
Treat the Interface class like that with Python type annotations
A set of script files that do wordcloud in Python3
A Python script that compares the contents of two directories