EDIT: Using Python 3.7 | Win10
I have read through a lot of posts on dealing with paths in python. Attempting a little script that sorts all files and sub dir files into folders by date of last modified.
So far it works, all for one issue, when obtaining the sub directory it returns with \ at the beginning thus making the final dir string base_dir/\subdir.
This means the first set of files gets copied just fine but anything in a subdirectory fails.
Getting a headache trying to figure out how to stop this happening. Hopefully after a screen break I will figure it out but just in case if any wizards on here can help would be greatly appreciated.
code:
import os
import time
import datetime
import shutil
from typing import List, Tuple
SORT_DIR = r'to_sort/'
def date_from_seconds(file_stats):
"""
Takes an os.stats variable and return a date
Using the seconds elapsed since last modification
"""
seconds = time.ctime(file_stats.st_mtime)
date_filter = datetime.datetime.strptime(seconds, '%a %b %d %H:%M:%S %Y')
date_to_return = f'{date_filter.day}-{date_filter.month}-{date_filter.year}'
return date_to_return
def sort_files(path_directory: str, file_list: List[Tuple]):
"""
Lists the files in the sort directory
Uses recursion to obtain files from subdirectories
Copies files to a directory named according to their last modified date
"""
content_dir: List[str] = os.listdir(path_directory)
for filename in content_dir:
path_file = os.sep.join([path_directory, filename])
if os.path.isdir(path_file):
sort_files(path_file, file_list)
else:
try:
stats = os.stat(path_file)
date = date_from_seconds(stats)
file_list.append((path_directory, filename, date))
os.makedirs(date, exist_ok=True)
print(f'{path_directory}{filename}')
shutil.copy(f'{path_directory}{filename}', f'{date}/{filename}')
except Exception as _err:
print(_err)
continue
files: List[Tuple] = []
sort_files(SORT_DIR, files)
print(files)
output:
subdir_sort.py
to_sort/1001001.jpg
to_sort/1001002.jpg
to_sort/1001003.jpg
to_sort/\subdir1002007.jpg
[Errno 2] No such file or directory: 'to_sort/\\subdir1002007.jpg'
to_sort/\subdir1002010.jpg
[Errno 2] No such file or directory: 'to_sort/\\subdir1002010.jpg'
to_sort/\subdir1002021.jpg
[Errno 2] No such file or directory: 'to_sort/\\subdir1002021.jpg'
[('to_sort/', '1001001.jpg', '21-9-2020'), ('to_sort/', '1001002.jpg', '21-9-2020'), ('to_sort/', '1001003.jpg', '21-9-2020'), ('to_sort/\\subdir', '1002007.jpg', '16-9-2020'), ('to_sort/\\subdir', '1002010.jpg', '16-9-2020'), ('to_sort/\\subdir', '1002021.jpg', '16-9-2020')]
Process finished with exit code 0
Thank You!
EDIT: Answer from orlevii worked a charm..here is the corrected code:
def sort_files(path_directory: str, file_list: List[Tuple]):
"""
Lists the files in the sort directory
Uses recursion to obtain files from subdirectories
Copies files to a directory named according to their last modified date
"""
for data in os.walk(SORT_DIR):
dir_path, folders, files = data
print(dir_path)
print(files)
for file in files:
try:
dir_for_stat = f'{dir_path}\{file}'
stats = os.stat(dir_for_stat)
date = date_from_seconds(stats)
file_list.append((dir_path, file, date))
os.makedirs(date, exist_ok=True)
print(f'{dir_path}\{file}')
shutil.copy(f'{dir_path}\{file}', f'{date}\{file}')
except Exception as _err:
print(_err)
continue
What OS are you running on?
I see you are concating your paths with:
path_file = os.sep.join([path_directory, filename])
Which is wrong if you are on Windows.
For it to work properly, you can use
path_file = os.path.join(path_directory, filename)
2nd, Is there any reason you implemented the scan yourself?
You can use os.walk
for getting all the files/directories starting from a given root path:
import os
SORT_DIR = 'to_sort'
for data in os.walk(SORT_DIR): # where to start searching
dir_path, folders, files = data
# Now do whatever you want with the `files` variable