0

I want to create a folder after an hour of the current time in python. I know how to get the current time and date and to create a folder. But how to create a folder at a time specified by me. Any help would be appreciated.

from datetime import datetime
from datetime import timedelta 
import os
while True:
    now = datetime.now ()
    #print(now.strftime("%H:%M:%S"))
    y = datetime.now () + timedelta (hours = 1)
    #print(y.strftime("%H:%M:%S"))
    if now== y:
      os.makedirs (y.strftime ("%H/%M/%S"))

will this work?

EDIT :- I have to run the code continuously i.e. creating folders at every instant of time

S123
  • 11
  • 5
  • If you plan on doing something else in the mean while (I guess you do) you will need some thread to wait for the time to pass and then create your folder – Eypros Jul 02 '18 at 08:38
  • U can use `time.sleep()` to keep your thread active – letmecheck Jul 02 '18 at 08:39

3 Answers3

2

Try this simple code

import os
import time

while True:    
  time.sleep(3600) # pending for 1 hour (3600 seconds)
  os.makedirs(your directory) # create the directory

EDIT (using parallel programming)

import os
import time
from datetime import datetime
from multiprocessing import Pool

def create_folder(now):
  # you can manipulate variable "now" as you wish
  time.sleep(3600) # pending for 1 hour (3600 seconds)
  os.makedirs(your directory) # create the directory
  return

while True:
  pool = Pool()
  now = datetime.now()    
  result = pool.apply_async(create_folder, [now]) # asynchronously evaluate 'create_folder(now)'

this may consume many of your computer resources

Rafid
  • 641
  • 1
  • 8
  • 20
0

check this post for better explanation,you can create a function which will run after given time and you can use this function for creating a folder by simple one line code os.makedirs("path\directory name") Python - Start a Function at Given Time

Vikas Gautam
  • 441
  • 8
  • 22
0

to create multiple folders after every 60 sec, folders like New1, New2,...

  import time

    while True:
        time_Begin = time.time()

        print("Creating Folder....")
        # CODE FOR CREATING FOLDER AND CONDITION

        time_End = time.time()
        time_Elapsed = time_End - time_Begin
        time.sleep(60-time_Elapsed)

Until external process not done

import time
import random


def creatingFolder():
    while externalProcess() != 30:
            timeBegin = time.time()

            print("Creating Folder....", timeBegin)

            timeEnd = time.time()
            timeElapsed = timeEnd - timeBegin
            time.sleep(5-timeElapsed)


def externalProcess():
    return random.randint(1, 30)

creatingFolder()
  • I have to create a folder an hour later for the time right now and continue this till my external process doesn't gets over. – S123 Jul 02 '18 at 10:20