Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
more looping help?
#4
first of all it's my mistake. it should be
for month in range(1, 13):
    folder = f'{year}-{month:0>2}'
    os.makedirs(os.path.join('.', folder))
just FYI, this folder = folder = f'{year}-{month:0>2}' is called multiple assignments and in fact it's equivalent to execute:

folder = f'{year}-{month:0>2}'
folder = f'{year}-{month:0>2}'
in general case if you want assign value to both foo and bar variables you can do foo = bar = value
>>> foo = bar = 'some value'
>>> foo
'some value'
>>> bar
'some value'
>>>
(Oct-01-2018, 09:15 AM)pcsailor Wrote: * How do I get past the point of just staring at a blank screen having no real idea how to start?
well, I guess it's the experience. In general case you need to have algorithm (i.e. what and how you want to do). In this case - you have single year, and want to iterate over months (finite number of iterations over pre-defined range of numbers). So it suggest using for loop. you want certain format of the folder name, so using string formatting mini-language is self-evident.
(Oct-01-2018, 09:15 AM)pcsailor Wrote: * Where did month come from? It's not a variable like 'year' which uses the datetime module.
both year and month are variables/names. Neither comes from datetime module. you can and should use any [descriptive] name you like.
(Oct-01-2018, 09:15 AM)pcsailor Wrote: * Why is the f-string using 2 folder-equals (folder = folder = f')? I've never seen that.
that's my mistake. see correct code above
(Oct-01-2018, 09:15 AM)pcsailor Wrote: * What are you joining with os..join? What is the '.' pointing to, current folder? If so, where's the backslash (.\)?
'.' it will point to same folder where it points to in your code. os.path.join just creates the proper path given the OS using os-specific correct separator.
By the way, if you are going to use datetime module, this will also work

import os
from datetime import date
today = date.today()
for month in range(1, 13):
    folder = today.replace(month=month).strftime('%Y-%m')
    os.makedirs(os.path.join('.', folder))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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