今天工作需要,需要动态建立一堆文件夹,就想着用Python 来做,Python 有两种用法,os.mkdir()与os.makedirs();用法也有差异,试着比较了一下效率,发现差别不是一般的大;提供出来供大家参考。
用法:
os.mkdir(path),功能是一级一级的创建目录,前提是前面的目录已存在,如果不存在会报异常;然后是os.makedirs(path),可以一次创建多级目录,哪怕中间目录不存在也能正常的(替你)创建。
效率:
附件:编码
用os.makedirs()
#!/usr/bin/python3
# -*- coding:< utf-8 >-*-
"""
This is only a study of python application for create more directory once,
made by Famous Zhang @ 2022.02.23
"""
import os
import shutil
import pandas as pd
import timeit
start = timeit.default_timer()
def makedirectory(file_name):
"""
"create the directory as need, if exists then pass "
"""
if not os.path.exists(file_name):
os.makedirs(file_name)
else:
pass
def copytofile(filename,file_path):
"""
"copy the special file to other directorys"
"""
shutil.copy(filename,file_path) # copyfile
def noticetxt():
"""
"make the notice text"
"""
filenm=os.getcwd()+'\\'+'注意事项.txt'
fo=open(filenm,"w")
fo.write("项目机密文件,严禁外传,违者必究")
fo.close
return filenm
def readname():
"""
"# 从filename.xlsx 的sheet1 读取相应的文件夹名字"
"""
dd=pd.read_excel(os.getcwd()+"\\"+"filename.xlsx",sheet_name="Sheet1",header=0,keep_default_na=False)
return dd
if __name__ == '__main__':
"""
"main procedure for those program "
"""
para=readname() # 从filename.xlsx 的sheet1 读取相应的文件夹名字
filenames=para['m_name'].values.tolist() # 要创建的文件夹名字, DataFrame 列转为list
childfilenames=para['c_name'].values.tolist() # 要创建的子文件夹名字
curpath=os.getcwd() # 获取当前路径
file=noticetxt() # 产生要Copy 到各个文件夹的共性说明文字,也可以直接指定需要Copy 的文件绝对路径
for i in filenames: # 循环建立列表中的文件夹名字
for j in childfilenames: # 建立每个文件夹下面的子文件夹
makedirectory(curpath+"\\"+i+"\\"+j)
copytofile(file,curpath+"\\"+i+"\\"+j)
copytofile(file,curpath+"\\"+i)
print("Make files ok,Pls check it!")
end = timeit.default_timer()
print("用时"+ str((end-start))+"秒")
用os.mkdir()
#!/usr/bin/python3
# -*- coding:< utf-8 >-*-
"""
This is only a study of python application for create more directory once,
made by Famous Zhang @ 2022.02.23
"""
import os
import shutil
import pandas as pd
import timeit
start = timeit.default_timer()
def makedirectory(file_name):
"""
"create the directory as need, if exists then pass "
"""
if not os.path.exists(file_name):
os.mkdir(file_name)
else:
pass
def copytofile(filename,file_path):
"""
"copy the special file to other directorys"
"""
shutil.copy(filename,file_path) # copyfile
def noticetxt():
"""
"make the notice text"
"""
filenm=os.getcwd()+'\\'+'注意事项.txt'
fo=open(filenm,"w")
fo.write("项目机密文件,严禁外传,违者必究")
fo.close
return filenm
def readname():
"""
"# 从filename.xlsx 的sheet1 读取相应的文件夹名字"
"""
dd=pd.read_excel(os.getcwd()+"\\"+"filename.xlsx",sheet_name="Sheet1",header=0,keep_default_na=False)
return dd
if __name__ == '__main__':
"""
"main procedure for those program "
"""
para=readname() # 从filename.xlsx 的sheet1 读取相应的文件夹名字
filenames=para['m_name'].values.tolist() # 要创建的文件夹名字, DataFrame 列转为list
childfilenames=para['c_name'].values.tolist() # 要创建的子文件夹名字
curpath=os.getcwd() # 获取当前路径
file=noticetxt() # 产生要Copy 到各个文件夹的共性说明文字,也可以直接指定需要Copy 的文件绝对路径
for i in filenames: # 循环建立列表中的文件夹名字
makedirectory(curpath+"\\"+i)
copytofile(file,curpath+"\\"+i) # copy to the directory as you chose
for j in childfilenames: # 建立每个文件夹下面的子文件夹
makedirectory(curpath+"\\"+i+"\\"+j)
#copytofile(file,curpath+"\\"+i+"\\"+j)
print("Make files ok,Pls check it!")
end = timeit.default_timer()
print("用时"+ str((end-start))+"秒")