Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

Jongskie M.

Technical User
Dec 30, 2023
24
AE
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:"
[highlight #CC0000]current_time = yesterday.strftime('%Y\%b %Y\%Y-%m-%d')[/highlight]
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.
 
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'
 
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
 
Using spaces in directory or file names is not a good decision.
This doesn't work properly in windows:
[pre]mkdir 2024\Jan 2024\2024-01-11[/pre]
but quotation marks are required:
[pre]mkdir 2024\"Jan 2024"\2024-01-11[/pre]
 

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,
 
 https://files.engineering.com/getfile.aspx?folder=a78c3375-a3b3-4e97-b411-44954ad75e42&file=subfolders.JPG
Jongskie said:
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.
 
Hi Mikrom,

Indeed, you got it[thumbsup2]
>>> [highlight #FCE94F]current_time = yesterday.strftime('%Y\\"%b %Y\"\%Y-%m-%d')[/highlight]

Thank you so much
 
Hi mikrom,

How to encode the if.. else statement(say "Folder Created" else Folder Already Exist! on the last subfolder creation(2024-01-11).
 
if you want to check if folder exists, you can use one of these methods
[pre]os.path.isdir(path)[/pre]
[pre]os.path.exists(path)[/pre]
 

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,
 
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 !
 
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.
 
Jongskie said:
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
 
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 !")

[highlight #FCE94F]my_path = "2024\\Jan_2024\\2024-01-11" <-- the declaration should be updated everyday[/highlight]
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)
 
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
 
Hi mikrom,

Great thanks for this code. you help a lot.

regards,
 
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:
 
For example if I want to create the subdirectory
[pre]2024\"Feb 2024"\2024-02-01[/pre]
in the directory
[pre]U:\tmp[/pre]
i.e. the full path would be
[pre]U:\tmp\2024\"Feb 2024"\2024-02-01[/pre]

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)
 
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,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top