def delete_no_checkpoints(parent): if os.path.isdir(parent): document = [] for p in os.listdir(parent): try: document.append(p) except: print("not document~") d = os.path.join(parent, p) # print(d) if (os.path.isdir(d) == True): delete_no_checkpoints(d) print("----") print("document:", document) if (len(document) > 0): old_path_name = parent.split("\\")[-1] print("old_path_name:", old_path_name) # change = input("是否需要删除(y/n)?") # if (change == 'y'): try: # 判断后缀是否在集合里,如果没有后缀,那么就是文件夹了 if 'pth' not in document and 'pt' not in document and 'checkpoint' not in document and 'progress.txt' in document: for doc in document: os.remove(os.path.join(old_path_name, doc)) shutil.retree(old_path_name) print("old_path_name:", old_path_name) print("删除成功!") except Exception as e: print("delete e:", e)
def rmr(**kwargs): if os.path.isdir(kwargs['params']): shutil.retree(kwargs['params']) # if it's a path elif os.path.isfile(kwargs['params']): os.remove(kwargs['params']) #if it's a file else: print("not avalable to remove")
'MPICH2':'http://www.mcs.anl.gov/research/projects/mpich2/downloads/tarballs/mpich2-current.tar.gz', 'FFTW3':'ftp://ftp.fftw.org/pub/fftw/fftw-3.3.2.tar.gz', 'LAPACK':'http://www.netlib.org/lapack/lapack-3.4.1.tgz', 'CBLAS':'http://www.netlib.org/clapack/cblas.tgz', 'WCSLIB':'ftp://ftp.atnf.csiro.au/pub/software/wcslib/wcslib.tar.bz2', 'NETCDF':'http://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-4.2.1.1.tar.gz' } print URL SUB_DIR = {} TAR_DIR = MAIN_DIR+'/tarballs' for k,v in URL.items(): urllib.urlretrieve(v,TAR_DIR) tar = tarfile.open(v.split('/')[-1]) if tar.member[0].isdir(): tar.extractall(path=MAIN_DIR) SUB_DIR.update({k,MAIN_DIR+'/'+tar.members[0].name}) else: SUB_DIR.update({k,MAIN_DIR+k}) os.makedirs(SUB_DIR) tar.extractall(path=SUB_DIR) shutil.retree(tar.name) tar.close() print SUB_DIR #os.system('git clone [email protected]:/MWA_Tools') #os.system('git clone [email protected]:/RTS') #os.system('git clone [email protected]:/CASA_Cals')
def build_and_run(code, lang): result = {'build': None, 'run': None, 'error': None} source_file_parent_dir_name = uuid.uuid4() source_file_host_dir = "%s/%s" % (TEMP_BUILD_DIR, source_file_parent_dir_name) source_file_guest_dir = "/test/%s" % (source_file_parent_dir_name) make_dir(source_file_host_dir) with open("%s/%s" % (source_file_host_dir, SOURCE_FILE_NAMES[lang]), 'w') as source_file: source_file.write(code) try: client.containers.run(image=IMAGE_NAME, command="%s %s" % (BUILD_COMMANDS[lang], SOURCE_FILE_NAMES[lang]), volumes={ source_file_host_dir: { 'bind': source_file_guest_dir, 'mode': 'rw' } }, working_dir=source_file_guest_dir) print("source built") result['build'] = 'ok' except ContainerError as e: result['build'] = str(e.stderr, 'utf-8') shutil.rmtree(source_file_host_dir) return result try: log = client.containers.run( image=IMAGE_NAME, command="%s %s" % (EXECUTE_COMMANDS[lang], BINARY_NAMES[lang]), volumes={ source_file_host_dir: { 'bind': source_file_guest_dir, 'mode': 'rw' } }, working_dir=source_file_guest_dir) log = str(log, 'utf-8') print(log) result['run'] = log except ContainerError as e: result['run'] = str(e.stderr, 'utf-8') shutil.retree(source_file_host_dir) return result shutil.rmtree(source_file_host_dir) return result
# 函数实现:os.mkdirs(name,mode = 0o777) 推荐 import os os.mkdirs(r'D:\demo\mr\demo\Navigator97') # 删除目录 # 语法:os.rmdir(path(相对路径或者绝对路径)) 删除空目录 import os if not os.path.exists(r'D:\demo\mr\demo\Navigator97'): os.rmdir(r'D:\demo\mr\demo\Navigator97') # 删除目录 print("目录删除成功") else: print("目录不存在!") # 删除不为空的目录:需要使用Python内置的shutil模块 # 语法:shutil.rmtree(path) import shutil shutil.retree(r'D:\demo') # 删除不为空的目录 # 遍历目录 # 语法:os.walk(top[, topdown(确定遍历顺序,True自上而下遍历,False相反)][,onerror指定错误处理方式,默认为忽略][,followlinks(True指定在支持的系统上访问由符号链接(软链接)指向的目录)]) # 返回值:元组生成器对象(diepath字符串,dirnames列表,filenames列表) import os path = os.walk(r'H:\Python Program') for p in path: print(p, '\n') # 实例:遍历指定目录 import os path = r'C:/Python' print('【', path, "】目录下包含的文件和目录:") for root, dirs, files in os.walk(path, topdown=True): # 遍历指定目录 for name in dirs: print(os.path.join(root, name)) # 输出遍历到的目录
import os import sys import shutil path=os.path.dirname(os.path.abspath(__file__)) for models in ['srgcn','gcn']: modelpath=os.path.join(path,model) for dirname in next(os.walk(modelpath))[1]: if 'label' in dirname: shutil.retree(os.path.join(modelpath,dirname))
def clean_work_dir(solution_id): dir_name = os.path.join(config.work_dir, str(solution_id)) shutil.retree(dir_name)
import tensorflow as tf import numpy as np import os import shutil decode = False resume_training = True save_dir = '_' + __file__[__file__.rfind('/') + 1:__file__.rfind('.')] if (not decode) and (not resume_training): if os.path.exists(save_dir): shutil.retree(save_dir) os.makedirs(save_dir) graph = tf.Graph() with graph.as_default(): with tf.variable_scope('variable'): w = tf.get_variable(name='w', initializer=tf.constant([0]), dtype=tf.int32) rand_inc = tf.random_uniform(minval=1, maxval=3, dtype=tf.int32, shape=[1]) update_w = tf.assign_add(w, rand_inc) saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=30) if decode: with tf.Session(graph=graph) as sess: ckpt = tf.train.get_checkpoint_state(save_dir)