×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to create previous date folder, declaring strftime with space between %b and %Y

How to create previous date folder, declaring strftime with space between %b and %Y

How to create previous date folder, declaring strftime with space between %b and %Y

(OP)
Hi am a newbie in phyton, anybody could help me because am declaring strftime on how to put blank spaces between the dates without hypenations, as of now I'm making a script that creates a folder based on previous time.

Please check my code.
from datetime import datetime, date

oneday = datetime.timedelta(days-1)
yesterday = datetime.date.today() - oneday
path = "G:"
current_time = yesterday.strftime('%Y\%b %Y\%Y-%m-%d')
command = mkdir {0}".format(current_time)
os.chdir(path)
os.system(command)

the script output should create directory as follows:
2024
Jan 2024 <== subfolder of 2024
2024-01-11<==subfolder of Jan 2024

Thanks.

RE: How to create previous date folder, declaring strftime with space between %b and %Y

But the code you posted does not work for me. Already on the second line I get error:
AttributeError: type object 'datetime.datetime' has no attribute 'timedel'

RE: How to create previous date folder, declaring strftime with space between %b and %Y

This works

CODE

>>> from datetime import date, timedelta
>>> yesterday = date.today() - timedelta(days=1)
>>> print(yesterday)
2024-01-11
>>> current_time = yesterday.strftime('%Y\%b %Y\%Y-%m-%d')
>>> print(current_time)
2024\Jan 2024\2024-01-11
>>> command = "mkdir {0}".format(current_time)
>>> print(command)
mkdir 2024\Jan 2024\2024-01-11 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

Using spaces in directory or file names is not a good decision.
This doesn't work properly in windows:
mkdir 2024\Jan 2024\2024-01-11 
but quotation marks are required:
mkdir 2024\"Jan 2024"\2024-01-11 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)

Thanks for the input mikrom, i will use to integrate this script soon in my securecrt emulator, are there any options on how to declare that function.
By the way how to declare -> mkdir 2024\"Jan 2024"\2024-01-11

Regards,

RE: How to create previous date folder, declaring strftime with space between %b and %Y

Quote (Jongskie)


By the way how to declare -> mkdir 2024\"Jan 2024"\2024-01-11

For example like this

CODE

>>> from datetime import date, timedelta
>>> yesterday = date.today() - timedelta(days=1)
>>> current_time = yesterday.strftime('%Y\\"%b %Y\"\%Y-%m-%d')
>>> print(current_time)
2024\"Jan 2024"\2024-01-11
>>> command = "mkdir {0}".format(current_time)
>>> print(command)
mkdir 2024\"Jan 2024"\2024-01-11 

But I personally would use rather this

CODE

>>> current_time = yesterday.strftime('%Y\%b_%Y\%Y-%m-%d')
>>> print(current_time)
2024\Jan_2024\2024-01-11
>>> command = "mkdir {0}".format(current_time)
>>> print(command)
mkdir 2024\Jan_2024\2024-01-11 

Because, if you name your folders with names containing spaces like Jan 2024, then to access the folders you will have everytime to bother with quotes. If you use instead Jan_2024 then it will be simpler.

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)
Hi Mikrom,

Indeed, you got itthumbsup2
>>> current_time = yesterday.strftime('%Y\\"%b %Y\"\%Y-%m-%d')

Thank you so much

RE: How to create previous date folder, declaring strftime with space between %b and %Y

Hi Jongskie,
You are welcome

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)
Hi mikrom,

How to encode the if.. else statement(say "Folder Created" else Folder Already Exist! on the last subfolder creation(2024-01-11).

RE: How to create previous date folder, declaring strftime with space between %b and %Y

if you want to check if folder exists, you can use one of these methods
os.path.isdir(path) 
os.path.exists(path) 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)

Hi mikrom,

os.path.isdir(path)
os.path.exists(path)


how to insert/ declare that function on below code.
>>> current_time = yesterday.strftime('%Y\%b_%Y\%Y-%m-%d')
>>> print(current_time)
2024\Jan_2024\2024-01-11
>>> command = "mkdir {0}".format(current_time)
>>> print(command)
mkdir 2024\Jan_2024\2024-01-11

regards,

RE: How to create previous date folder, declaring strftime with space between %b and %Y

Hi Jongskie,

both functions return True or False, so you can use them in if-statement like this:

CODE

import os

def check_path(the_path):
    if os.path.isdir(the_path):
       print("path", the_path, "is a directory")
    else:
       print("path", the_path, "is not a directory !")

my_path = "2024\\Jan_2024\\2024-01-11"
other_path = "2004\\spam\\eggs"

# create a directory
my_cmd = "mkdir " + my_path
print("Now executing this command to create directory:\n\t" + my_cmd)
os.system(my_cmd)

# check directories
check_path(my_path)
check_path(other_path) 

The output:

CODE

C:\> python3 jongskie.py
Now executing this command to create directory:
        mkdir 2024\Jan_2024\2024-01-11
path 2024\Jan_2024\2024-01-11 is a directory
path 2004\spam\eggs is not a directory ! 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)
Hi micron,

Are there any way to automate the date completely from my_path = "2024\\Jan_2024\\2024-01-11" not a string.

Example :
Today - 2024\Jan_2024\\2024-01-11
Tomorrow - 2024\Jan_2024\\2024-01-12

I appreciate your efforts.

RE: How to create previous date folder, declaring strftime with space between %b and %Y

Quote (Jongskie)


Are there any way to automate the date completely from my_path = "2024\\Jan_2024\\2024-01-11" not a string.

Example :
Today - 2024\Jan_2024\\2024-01-11
Tomorrow - 2024\Jan_2024\\2024-01-12
I don't understand what you mean

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)
Hi micron,

Highlighted below should automate the date everyday as a variable.


import os

def check_path(the_path):
if os.path.isdir(the_path):
print("path", the_path, "is a directory")
else:
print("path", the_path, "is not a directory !")

my_path = "2024\\Jan_2024\\2024-01-11" <-- the declaration should be updated everyday
other_path = "2004\\spam\\eggs"

# create a directory
my_cmd = "mkdir " + my_path
print("Now executing this command to create directory:\n\t" + my_cmd)
os.system(my_cmd)

# check directories
check_path(my_path)
check_path(other_path)

RE: How to create previous date folder, declaring strftime with space between %b and %Y

if your program runs everyday then it can declare the path variable as follows

CODE

>>> from datetime import date
>>> my_path = date.today().strftime('%Y\\%b_%Y\%Y-%m-%d')
>>> print(my_path)
2024\Jan_2024\2024-01-20 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)
Hi mikrom,

Great thanks for this code. you help a lot.

regards,

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)
Hi mikrom,

From the code below, how to create same folder name in different path.

CODE -->

>>> from datetime import date, timedelta
>>> yesterday = date.today() - timedelta(days=1)
>>> current_time = yesterday.strftime('%Y\\"%b %Y\"\%Y-%m-%d')
>>> print(current_time)
2024\"Feb 2024"\2024-02-01
>>> command = "mkdir {0}".format(current_time)
>>> print(command)
mkdir 2024\"Feb 2024"\2024-02-01 


Output:
2024\Feb 2024\2024-02-01 <=== "C:"
2024\Feb 2024\2024-02-01 <=== "D:"

RE: How to create previous date folder, declaring strftime with space between %b and %Y

For example if I want to create the subdirectory
2024\"Feb 2024"\2024-02-01 
in the directory
U:\tmp 
i.e. the full path would be
U:\tmp\2024\"Feb 2024"\2024-02-01 

CODE

>>> my_path = "U:\\tmp\\"
>>> command = "mkdir {0}{1}".format(my_path, current_time)
>>> print(command)
mkdir U:\tmp\2024\"Feb 2024"\2024-02-01 

and then execute the command using os.system()

CODE

>>> import os
>>> os.system(command) 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)

Quote:

mikrom (Programmer)
For example if I want to create the subdirectory
2024\"Feb 2024"\2024-02-01
in the directory
U:\tmp
i.e. the full path would be
U:\tmp\2024\"Feb 2024"\2024-02-01

what I mean is that, it will create a folder with different location/path, not in a subdirectory.

my_path0 = "U:\\tmp\\"
my_path1 = "V:\\tmp\\"

U:\tmp\2024\"Feb 2024"\2024-02-01
V:\tmp\2024\"Feb 2024"\2024-02-01

regards,

RE: How to create previous date folder, declaring strftime with space between %b and %Y

for example like this:

CODE

>>> import os
>>> from datetime import date, timedelta
>>> yesterday = date.today() - timedelta(days=1)
>>> current_time = yesterday.strftime('%Y\\"%b %Y\"\%Y-%m-%d')
>>> paths = ["U:\\tmp\\", "V:\\tmp\\"]
>>> for path in paths:
	command = "mkdir {0}{1}".format(path, current_time)
	os.system(command) 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)
am trying to reencode with print function to show the two folders, but it will display only the V:\tmp folder, & another thing the os.startfile() function should open both folder after it was been successfully created, is that possible?

CODE -->

>>> paths = ["U:\\tmp\\", "V:\\tmp\\"]
>>> for path in paths:
	command = "mkdir {0}{1}".format(path, current_time)
	os.system(command)
>>> print("Executing to create two folder directory namely :\n\n" + path)
>>> os.startfile(path) 

thank you so much mikrom am learning a lot.

regards,

RE: How to create previous date folder, declaring strftime with space between %b and %Y

Because you don't have the last two statements in the for-loop.
Place them into the loop and try again

CODE

>>> paths = ["U:\\tmp\\", "V:\\tmp\\"]
>>> for path in paths:
	command = "mkdir {0}{1}".format(path, current_time)
	os.system(command)
        print("Executing to create two folder directory namely :\n\n" + path)
        os.startfile(path) 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)
Hi, from the highlighted function below am expecting an result of U:\tmp\2024\Feb 2024\2024-02-01\ & V:\tmp\2024\Feb 2024\2024-02-01\ but the output are only pointing in U:\tmp\ and V:\tmp\ folder separately , how do you declare?

CODE -->

print("Executing to create two folder directory namely :\n\n" + path)
         os.startfile(path) 

thanks.

RE: How to create previous date folder, declaring strftime with space between %b and %Y

I made a typo above.
Instead of using
path 
in
os.startfile(path) 
you have to use
path + current_time 
or
"{0}{1}".format(path, current_time) 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

(OP)

Quote:

mikrom (Programmer)
Because you don't have the last two statements in the for-loop.
Place them into the loop and try again

Thank you mikrom for your suggestion, I segregated the path code, they are now opening using os.startfile() function, but the result of the os.startfile() is only in D:\tmp0\2024 & D:\tmp1\2024, the requirements will be pointed out directly inside the newly created folder, how's that? See snip thanks


CODE -->

paths = ["D:\\tmp0\\"]
paths1 = ["D:\\tmp1\\"]
for path in paths:
	command = "mkdir {0}{1}".format(path, current_time)
	os.system(command)
print("Executing to create first folder:\n" + path + current_time)
os.startfile(path)

for path1 in paths1:
	command = "mkdir {0}{1}".format(path1, current_time)
	os.system(command)
print("Executing to create second folder:\n" + path1 + current_time)
os.startfile(path1) 

RE: How to create previous date folder, declaring strftime with space between %b and %Y

Then try to solve your problem
- first, determine what the directory path should look like so that os.startfile(path) could open it
- next, correct the path building in your code

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close