Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
more looping help?
#3
for month in range(1, 13):
    folder = folder = f'{year}-{month:0>2}'
    os.makedirs(os.path.join('.', folder))
Thanks Buran. That worked.
Can you explain the code to me and, more importantly, your thought process to arrive at this code?
* How do I get past the point of just staring at a blank screen having no real idea how to start?
* Where did month come from? It's not a variable like 'year' which uses the datetime module.
* Why is the f-string using 2 folder-equals (folder = folder = f')? I've never seen that.
* What are you joining with os..join? What is the '.' pointing to, current folder? If so, where's the backslash (.\)?

Thanks for the help.
I quickly (for me Pray ) coded up a script to create a complete folder-subfolder structure for a tax year. This will be a yearly time saver and I'm quite proud of myself Dance LOL
Phil

BTW, for anybody here for future reference.

required dependencies:
import os
import datetime
year = datetime.now().year
The above forLoop replaces these lines of code:
os.makedirs('.\\' + str(year) + '-01')
os.makedirs('.\\' + str(year) + '-02')
os.makedirs('.\\' + str(year) + '-03')
os.makedirs('.\\' + str(year) + '-04')
os.makedirs('.\\' + str(year) + '-05')
os.makedirs('.\\' + str(year) + '-06')
os.makedirs('.\\' + str(year) + '-07')
os.makedirs('.\\' + str(year) + '-08')
os.makedirs('.\\' + str(year) + '-09')
os.makedirs('.\\' + str(year) + '-10')
os.makedirs('.\\' + str(year) + '-11')
os.makedirs('.\\' + str(year) + '-12')
Phil
Reply


Messages In This Thread
more looping help? - by pcsailor - Oct-01-2018, 04:32 AM
RE: more looping help? - by buran - Oct-01-2018, 06:45 AM
RE: more looping help? - by pcsailor - Oct-01-2018, 09:15 AM
RE: more looping help? - by buran - Oct-01-2018, 10:20 AM
RE: more looping help? - by stullis - Oct-02-2018, 12:05 AM
RE: more looping help? - by pcsailor - Oct-03-2018, 05:05 AM
RE: more looping help? - by buran - Oct-03-2018, 06:04 AM
RE: more looping help? - by wavic - Oct-03-2018, 06:22 AM

Forum Jump:

User Panel Messages

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