周逸(第十单元)(实战)

实战01

import time
def create():
global name
localTime = time.strftime("%Y%m%d%H%M%S", time.localtime())
name = localTime + '.txt'
with open(name,'a') as file:
pass
if __name__ == '__main__':
amount = int(input('请输入需要生成的文件数:'))
for i in range(amount):
create()
print('file ' + str(i+1) +':' + name)
time.sleep(1)
print('生成文件成功!')

结果

请输入需要生成的文件数:2
file 1:20221104193105.txt
file 2:20221104193106.txt
生成文件成功!

实战02

import os # 文件或目录模块

path = 'C:\\Users\\julie.wang\\PycharmProjects\\'

def folders(number):
for i in range(1, number + 1):
folder_name = path + str(i)
if os.path.exists(folder_name):
print("already exist")
else:
os.makedirs(folder_name)
if os.path.exists(folder_name):
print("successful")
print(os.getcwd())

if __name__ == '__main__':
number = int(input("要生成的文件夹数量"))
folders(number)

posted @ 2022-11-04 19:34  wzsyx  阅读(25)  评论(0)    收藏  举报