使用变量作为目录的名称

时间:2016-11-04 02:59:42

标签: python

我试图在python中编写一个脚本来创建一个名为当前时间戳的文件夹,但是我真的不知道这样做的语法。到目前为止,我有这个:

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

当我运行时,我遇到了错误。

P.S。我是初学者,请不要咬人。

1 个答案:

答案 0 :(得分:2)

时间戳需要转换为str。然后需要将str作为参数传递给os函数:

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
相关问题