Creating folder in python [duplicate]


Creating folder in python [duplicate]



This question already has an answer here:



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



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





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 2 at 8:38





U can use time.sleep() to keep your thread active
– Mohan Babu
Jul 2 at 8:39


time.sleep()




3 Answers
3



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 2 at 10:20



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





Could yo add some explanation about why and how your code is answering the question?
– anothernode
Jul 2 at 9:11





I have to run the code continuously i.e. creating folders at every instant of time
– S123
Jul 2 at 9:43





then you can put while True: between those code
– Rafid
Jul 2 at 9:50


while True:





Your code or mine? Can you show the code?
– S123
Jul 2 at 10:12






my code, i have edited the example
– Rafid
Jul 2 at 10:14



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("pathdirectory name")
Python - Start a Function at Given Time

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages