Henry Poole
Henry Poole

Reputation: 1

Use a variable as a name for a directory

I'm trying to code a script in python that creates a folder entitled the current timestamp, but don't really know the syntax to do it. So far I have this:

timestamp = int(time.time()) #fetches timestamp
if not os.path.exists([timestamp]): #creates new folder titled the current timestamp
    os.makedirs([timestamp])
os.chdir([timestamp]) #navigates to new folder

When I run this, I run into errors.

P.S. I am a beginner, please don't bite.

Upvotes: 0

Views: 41

Answers (1)

Dan D.
Dan D.

Reputation: 74655

The timestamp needs to be converted to a string. and then that string need to be passed as the argument to the os functions:

timestamp = str(int(time.time())) #fetches timestamp
if not os.path.exists(timestamp): #creates new folder titled the current timestamp
    os.makedirs(timestamp)
os.chdir(timestamp) #navigates to new folder

Upvotes: 2

Related Questions