文件操作
- 进入某文件夹,os.chdir(gammaFile)
- 判断某文件是否是文件夹,os.path.isdir(gammaFile)
- 得到当前路径,os.getcwd()
- 新建某文件夹,
1 if not os.path.exists(floder_name_1): 2 os.makedirs(floder_name_1)
- 复制文件, shutil.copy(fig_file, '../' + floder_name)
复制文件
1 #!/usr/bin/python3 2 3 import os 4 import shutil 5 6 os.chdir("./") 7 os.mkdir('./cal_result') 8 gamma_files = os.listdir() 9 10 for gamma_file in gamma_files: # loop gamma files 11 if os.path.isdir(gamma_file) and "gamma" in gamma_file: 12 os.mkdir('./cal_result/' + gamma_file) 13 os.chdir(gamma_file) 14 QQPRM_files = os.listdir() 15 for QQPRM_file in QQPRM_files: 16 os.mkdir('../cal_result/' + gamma_file + '/' + QQPRM_file) 17 os.chdir(QQPRM_file) 18 shutil.copy('./EXCITATION.DAT', '../../cal_result/' + gamma_file + '/' + QQPRM_file) 19 shutil.copy('./BM1.DAT', '../../cal_result/' + gamma_file + '/' + QQPRM_file) 20 shutil.copy('./BE2S.DAT', '../../cal_result/' + gamma_file + '/' + QQPRM_file) 21 shutil.copy('./Kprobility.DAT', '../../cal_result/' + gamma_file + '/' + QQPRM_file) 22 shutil.copy('./KDISM.DAT', '../../cal_result/' + gamma_file + '/' + QQPRM_file) 23 shutil.copy('./KDISS.DAT', '../../cal_result/' + gamma_file + '/' + QQPRM_file) 24 os.chdir('../') 25 os.chdir('../') 26 27 print('the end')
重命名文件
- os.rename(a, b) ,将 a 文件重命名为 b.