栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

创建名称为当前时间(年月日)的目录,在这个目录下创建名称为当前时间(年月日时分秒)的txt文件,并且输入一段内容

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建名称为当前时间(年月日)的目录,在这个目录下创建名称为当前时间(年月日时分秒)的txt文件,并且输入一段内容

import time
import os
import locale
locale.setlocale(locale.LC_CTYPE,'chinese')

t1 = time.strftime("%Y-%m-%d",time.localtime())
t2 = time.strftime("%Y-%m-%d %H%M%S",time.localtime())
print(t1)
print(t2)
os.chdir("C:\")
os.mkdir(t1)
os.chdir(t1)
filename = t2+".txt"

with open(filename,'w',encoding='utf-8') as file_object:
    file_object.write("Hello World!")
print(os.path.abspath(filename))

with open(filename,'r',encoding='utf-8') as file_object1:
    print(file_object1.readlines())
import time
import os
dir_name = time.strftime("%Y%m%d")
dir_path = os.path.join("C:\",dir_name)

print(dir_name)
print(dir_path)

os.mkdir(dir_path)
os.chdir(dir_path)

filename = time.strftime("%Y%m%d%H%M%S") +".txt"

with open(filename,'w',encoding='utf-8') as file_object:
    file_object.write(
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/714970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号