Python Forum
Create dual folder on different path/drive based on the date
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create dual folder on different path/drive based on the date
#1
Hello Team,

Below script creates a folder based from a previous date,
My question is,
1. How to declare the if/ else statement that says print("Folder created") else print("Folder already exists")
2. How to create folder in different drives

path = "c:\user\user\My Documents"
path1 = "d:\user\user\backup"

>>> current_time = yesterday.strftime('%Y\%b_%Y\%Y-%m-%d')
>>> print(current_time)
2024\Jan_2024\2024-01-20
>>> command = "mkdir {0}".format(current_time)
>>> print(command)
mkdir 2024\Jan_2024\2024-01-20
Larz60+ write Jan-21-2024, 11:41 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Modified for you this time. Please use BBCode tags on future posts
Reply
#2
This code snippet, from an app that I coded for making backups for files/directories may be of help to you.

for path, dirs, files in walk(SRC):
    for file in files:
        dst_path = Path(path.replace(ROOT, DST))
        if dst_path.exists():
            pass
        else:
            makedirs(dst_path)
        src_path = PurePath(Path(path).joinpath(Path(file)))
        dst_path = PurePath(dst_path.joinpath(Path(file)))
        print(f"Copying {file}")
        RW = read_write(src_path, dst_path)
        COUNT += 1
You'll also need:

from pathlib import Path, PurePath
from os import walk, makedirs

... and also read the docs before you start messing with the file system so that know what you're doing.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Make a function for create folders and don't use singel that way \ in path.
Look into pathlib.
yesterday is not defiend in code.
from pathlib import Path
from datetime import datetime, timedelta

def create_folder(path):
    if not path.exists():
        path.mkdir(parents=True)
        print(f"Folder created: {path}")
    else:
        print(f"Folder already exists: {path}")

if __name__ == '__main__':
    yesterday = datetime.now() - timedelta(days=1)
    folder_name = yesterday.strftime('%Y\%b_%Y\%Y-%m-%d')
    path_c = Path("C:/user/user/My Documents") / folder_name
    path_d = Path("D:/user/user/backup") / folder_name
    create_folder(path_c)
    create_folder(path_d)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 250 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
Question How to get a removable disc type in drive Daring_T 12 1,282 Feb-11-2024, 08:55 AM
Last Post: Gribouillis
  Python date format changes to date & time 1418 4 623 Jan-20-2024, 04:45 AM
Last Post: 1418
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 566 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  open python files in other drive akbarza 1 709 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  Integrating Google Drive in App Lahearle 0 478 Jul-19-2023, 05:51 PM
Last Post: Lahearle
  create a default path with idle to a specific directory greybill 0 882 Apr-23-2023, 04:32 AM
Last Post: greybill
  create new column based on condition arvin 12 2,255 Dec-13-2022, 04:53 PM
Last Post: jefsummers
  code to send attachments contained on the drive. stefanoste78 1 869 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,820 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020