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 SkipVought 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
21
0
1
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.
 
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)
 
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,
 
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)
 
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)
        [COLOR=#EF2929] os.startfile(path)[/color]

thanks.

 
I made a typo above.
Instead of using [pre][highlight #FCE94F]path[/highlight][/pre] in [pre]os.startfile([highlight #FCE94F]path[/highlight])[/pre] you have to use
[pre][highlight #FCE94F]path + current_time[/highlight][/pre]
or
[pre][highlight #FCE94F]"{0}{1}".format(path, current_time)[/highlight][/pre]
 
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
tmp_x0gdxp.jpg


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)
 
Then try to solve your problem
- first, determine what the directory path should look like so that [tt]os.startfile(path)[/tt] could open it
- next, correct the path building in your code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top