-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_files_using_pathlib.py
48 lines (31 loc) · 1.27 KB
/
move_files_using_pathlib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import pathlib
import datetime
import shutil
def clean_up_files():
source = r'C:\Users\mipc\Downloads'
target = r'C:\Users\mipc\Downloads\old'
# Create Path objects
target_dir = pathlib.Path(target)
source_dir = pathlib.Path(source)
# Create datetime object for today's date
today = str(datetime.date.today())
# Create target directory if necessary
if target_dir.exists():
print("target dir already exists")
else:
pathlib.Path.mkdir(target_dir)
print("created new dir")
# iterdir is pathlib's answer to os.listdir()
for filename in source_dir.iterdir():
# check to see last modification date of each file
modify_timestamp = filename.stat().st_mtime
modify_date = str(datetime.datetime.fromtimestamp(modify_timestamp))
# the date information is at the beginning of the string object, which is stored in a new object
modify_date = modify_date[0:10]
if modify_date == today:
# check if there's a file extension to skip folder names
if pathlib.PurePath(filename).suffix:
# move file to folder
shutil.move(filename, target_dir)
print(f"moved '{filename.parts[-1]}' to {target_dir}.")
clean_up_files()