I want to make a folder which contains my data which I read from excel and along with it also the current date-time stamp. So every time I run the code it will create new folder accordingly.
# here are 2 different files whose data I want to save in my folder
car_data = car_readin(files_data, 'car_sheetname')
bike_data = bike_readin(files_data, 'bike_sheetname')
# defined a function which can create a dynamic folder with automatic data readin(car_data,..) inside
def output_dir(vehicle_data):
# create dynamic name, like "title_vehicle_data_2022-09-20_13-44"
directory = os.path.join(base_directory, "Outputfolder"
dt.now().strftime('%Y-%m-%d_%H-%M-%S'))
# create 'dynamic' directory, if it does not exist
if not os.path.exists(directory):
os.makedirs(directory)
return directory
So in the parameter vehicle_data I can simply write whichever file I want to run in main script. eg. output_dir('car_data') then it will extract accordingly, with current Time-date stamp. If anyone has better solution please suggest.
CodePudding user response:
If I am correctly able to guess what you are trying to ask, you can simply paste strings together to get the name you want.
Recall that the argument to os.mkdir
is simply a string. Create a string which contains the name you want.
def output_dir(vehicle_data):
directory = "_".join("title", vehicle_data, dt.now().strftime('%Y-%m-%d_%H-%M-%S')
os.makedirs(directory, exist_ok=True)
return directory
If you call it with cars
, it will create a directory named title_cars_2022-10-06_20-55
if it doesn't already exist, and return this string.
As remarked on elsewhere, you don't need to prepend os.cwd()
; the computer already knows which directory is your current directory, and that's where it will create new files and directories. In fact, that's what "current directory" means. See also What exactly is current working directory?