0

I made this little python 3 script to create folders with day-month-year names, from 16-09-2020 to 16-10-2020:

import os

# starting values for day, month and year
parent_dir = 'C:\\Users\\locale\\Desktop\\DIRS\\'
day_int = 16
month_int = 9
year_int = 2020


def gen_day_str():
    if len(str(day_int)) == 1:
        day_str = '0' + str(day_int)
    elif len(str(day_int)) == 2:
        day_str = str(day_int)
    else:
        print('error! exiting...')
        exit()
def gen_month_str():
    if len(str(month_int)) == 1:
        month_str = '0' + str(month_int)
    elif len(str(month_int)) == 2:
        month_str = str(month_int)
    else:
        print('error! exiting...')
        exit()
def gen_year_str():
    year_str = str(year_int)
def gen_dir_name():
    gen_day_str()
    gen_month_str()
    gen_year_str()
    dir_name = day_str + '-' + month_str + '-' + year_str


while day_int <= 30:
    gen_dir_name()
    os.mkdir(parent_dir + dir_name)
    day_int += 1
day_int = 1
month_int = 10
while day_int <= 16:
    gen_dir_name()
    os.mkdir(parent_dir + dir_name)
    day_int += 1

print('folders created successfully!')
exit()

but when I run it i receive a NameError...

Traceback (most recent call last):
  File "folder_gen.py", line 36, in <module>
    gen_dir_name()
  File "folder_gen.py", line 32, in gen_dir_name
    dir_name = day_str + '-' + month_str + '-' + year_str
NameError: name 'day_str' is not defined

I don't understand why it would be necessary to define day_str (or month_str/year_str) before running the function

I tried to fix it by defining day_str, month_str and year_str in the start, like:

.
.
.
day_int = 16
month_int = 9
year_int = 2020
day_str = str(day_int)
month_str = '0' + str(month_int)
year_str = str(year_int)
.
.
.

But then the program created only one folder (16-09-2020) and exited.

Can someone please explain to me what am I doing wrong?

  • Inside your `gen_dir_name` function, you haven't defined `day_str` variable – Moinuddin Quadri Dec 29 '20 at 23:36
  • 1
    `day_str` only exists as a local variable inside the `gen_day_str()` function. It isn't accessible anywhere else. – John Gordon Dec 29 '20 at 23:40
  • Does this answer your question? [Variable not available to sub-function](https://stackoverflow.com/questions/41335752/variable-not-available-to-sub-function) - Actually, on second glance, it's close, but not quite the same. – wjandrea Dec 29 '20 at 23:42
  • You forget to `return` all the day_str, month_str, etc... – Daniel Hao Dec 29 '20 at 23:43
  • So declaring them as global variables inside their functions should fix it? I'll try that – monsieur51252 Dec 29 '20 at 23:43
  • Beside the point of the question, but check out [this code for generating a range of dates](https://stackoverflow.com/a/7274316/4518341). It's wayyy simpler. – wjandrea Dec 30 '20 at 00:15
  • You usually want to avoid declaring too many global variables. Good practice is to pass arguments to a function and return the result rather than reaching outside of the function scope for magic values. You might want to check some discussions concerning the usage of global variables, e.g. [this reddit post](https://www.reddit.com/r/Python/comments/25jzga/what_exactly_makes_global_variables_so_bad/) – DanDayne Dec 30 '20 at 00:45

1 Answers1

1

you have to return each of the created strings in the function, like this example:


def gen_year_str():
    return str(year_int)
    
def gen_dir_name():
   
    dir_name = gen_day_str() + '-' + gen_month_str() + '-' + gen_year_str()
    print(dir_name)

# Output:
16-09-2020
......
Daniel Hao
  • 4,922
  • 3
  • 10
  • 23