Python os.rename() Examples
The following are 30
code examples of os.rename().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
os
, or try the search function
.

Example #1
Source File: get_data.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 10 votes |
def get_cifar10(data_dir): if not os.path.isdir(data_dir): os.system("mkdir " + data_dir) cwd = os.path.abspath(os.getcwd()) os.chdir(data_dir) if (not os.path.exists('train.rec')) or \ (not os.path.exists('test.rec')) : import urllib, zipfile, glob dirname = os.getcwd() zippath = os.path.join(dirname, "cifar10.zip") urllib.urlretrieve("http://data.mxnet.io/mxnet/data/cifar10.zip", zippath) zf = zipfile.ZipFile(zippath, "r") zf.extractall() zf.close() os.remove(zippath) for f in glob.glob(os.path.join(dirname, "cifar", "*")): name = f.split(os.path.sep)[-1] os.rename(f, os.path.join(dirname, name)) os.rmdir(os.path.join(dirname, "cifar")) os.chdir(cwd) # data
Example #2
Source File: fsm.py From neural-pipeline with MIT License | 7 votes |
def pack(self) -> None: """ Pack all files in zip """ def rm_file(file: str): if os.path.exists(file) and os.path.isfile(file): os.remove(file) def rename_file(file: str): target = file + ".old" rm_file(target) if os.path.exists(file) and os.path.isfile(file): os.rename(file, target) self._check_files([self._weights_file, self._state_file]) rename_file(self._checkpoint_file) with ZipFile(self._checkpoint_file, 'w') as zipfile: zipfile.write(self._weights_file, os.path.basename(self._weights_file)) zipfile.write(self._state_file, os.path.basename(self._state_file)) zipfile.write(self._trainer_file, os.path.basename(self._trainer_file)) self.clear_files()
Example #3
Source File: file_util.py From glazier with Apache License 2.0 | 6 votes |
def Move(src: Text, dst: Text): """Move a file from src to dst. Python's os.rename doesn't support overwrite on Windows. Args: src: The full file path to the source. dst: The full file path to the destination. Raises: Error: Failure moving the file. """ try: Remove(dst) os.rename(src, dst) except OSError as e: raise Error('Failure moving file from %s to %s. (%s)' % (src, dst, str(e)))
Example #4
Source File: utils.py From ciocheck with MIT License | 6 votes |
def _rename_over_existing(src, dest): try: # On Windows, this will throw EEXIST, on Linux it won't. os.rename(src, dest) except IOError as err: if err.errno == errno.EEXIST: # Clearly this song-and-dance is not in fact atomic, # but if something goes wrong putting the new file in # place at least the backup file might still be # around. backup = "{0}.bak-{1}".format(dest, str(uuid.uuid4())) os.rename(dest, backup) try: os.rename(src, dest) except Exception as err: os.rename(backup, dest) raise err finally: try: os.remove(backup) except Exception as err: pass
Example #5
Source File: mcg_munge.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License | 6 votes |
def munge(src_dir): # stored as: ./MCG-COCO-val2014-boxes/COCO_val2014_000000193401.mat # want: ./MCG/mat/COCO_val2014_0/COCO_val2014_000000141/COCO_val2014_000000141334.mat files = os.listdir(src_dir) for fn in files: base, ext = os.path.splitext(fn) # first 14 chars / first 22 chars / all chars + .mat # COCO_val2014_0/COCO_val2014_000000447/COCO_val2014_000000447991.mat first = base[:14] second = base[:22] dst_dir = os.path.join('MCG', 'mat', first, second) if not os.path.exists(dst_dir): os.makedirs(dst_dir) src = os.path.join(src_dir, fn) dst = os.path.join(dst_dir, fn) print 'MV: {} -> {}'.format(src, dst) os.rename(src, dst)
Example #6
Source File: file.py From gnocchi with Apache License 2.0 | 6 votes |
def _store_new_measures(self, metric_id, data): tmpfile = tempfile.NamedTemporaryFile( prefix='gnocchi', dir=self.basepath_tmp, delete=False) tmpfile.write(data) tmpfile.close() path = self._build_measure_path(metric_id, True) while True: try: os.rename(tmpfile.name, path) break except OSError as e: if e.errno != errno.ENOENT: raise try: os.mkdir(self._build_measure_path(metric_id)) except OSError as e: # NOTE(jd) It's possible that another process created the # path just before us! In this case, good for us, let's do # nothing then! (see bug #1475684) if e.errno != errno.EEXIST: raise
Example #7
Source File: download.py From nmp_qc with MIT License | 6 votes |
def download_figshare(file_name, file_ext, dir_path='./', change_name = None): prepare_data_dir(dir_path) url = 'https://ndownloader.figshare.com/files/' + file_name wget.download(url, out=dir_path) file_path = os.path.join(dir_path, file_name) if file_ext == '.zip': zip_ref = zipfile.ZipFile(file_path,'r') if change_name is not None: dir_path = os.path.join(dir_path, change_name) zip_ref.extractall(dir_path) zip_ref.close() os.remove(file_path) elif file_ext == '.tar.bz2': tar_ref = tarfile.open(file_path,'r:bz2') if change_name is not None: dir_path = os.path.join(dir_path, change_name) tar_ref.extractall(dir_path) tar_ref.close() os.remove(file_path) elif change_name is not None: os.rename(file_path, os.path.join(dir_path, change_name)) # Download QM9 dataset
Example #8
Source File: handlers.py From jawfish with MIT License | 6 votes |
def rotate(self, source, dest): """ When rotating, rotate the current log. The default implementation calls the 'rotator' attribute of the handler, if it's callable, passing the source and dest arguments to it. If the attribute isn't callable (the default is None), the source is simply renamed to the destination. :param source: The source filename. This is normally the base filename, e.g. 'test.log' :param dest: The destination filename. This is normally what the source is rotated to, e.g. 'test.log.1'. """ if not callable(self.rotator): # Issue 18940: A file may not have been created if delay is True. if os.path.exists(source): os.rename(source, dest) else: self.rotator(source, dest)
Example #9
Source File: handlers.py From jawfish with MIT License | 6 votes |
def doRollover(self): """ Do a rollover, as described in __init__(). """ if self.stream: self.stream.close() self.stream = None if self.backupCount > 0: for i in range(self.backupCount - 1, 0, -1): sfn = self.rotation_filename("%s.%d" % (self.baseFilename, i)) dfn = self.rotation_filename("%s.%d" % (self.baseFilename, i + 1)) if os.path.exists(sfn): if os.path.exists(dfn): os.remove(dfn) os.rename(sfn, dfn) dfn = self.rotation_filename(self.baseFilename + ".1") if os.path.exists(dfn): os.remove(dfn) self.rotate(self.baseFilename, dfn) if not self.delay: self.stream = self._open()
Example #10
Source File: create_joint_gs.py From CAMISIM with Apache License 2.0 | 6 votes |
def create_gsa_mapping(path, metadata, sample_name, shuffle): """ Creates the binning gold standard/gsa mapping """ to_genome = name_to_genome(metadata) gsa_path = os.path.join(path, "anonymous_gsa.fasta") # count = 0 if not os.path.exists(gsa_path): gsa_path = os.path.join(path, "anonymous_gsa.fasta.gz") # if zipped with gzip.open(gsa_path,'r') as gsa: for line in gsa: if line.startswith('>'): count += 1 with gzip.open(gsa_path,'r') as gsa: gsa_temp = shuffle_anonymize(gsa, path, to_genome, metadata, sample_name, count, shuffle) else: with open(gsa_path,'r') as gsa: for line in gsa: if line.startswith('>'): count += 1 with open(gsa_path,'r') as gsa: gsa_temp = shuffle_anonymize(gsa, path, to_genome, metadata, sample_name, count, shuffle) os.rename(gsa_temp, gsa_path)
Example #11
Source File: update.py From CAMISIM with Apache License 2.0 | 6 votes |
def updateData(settings, type): """ Updates data in a folder, the existing folder is renamed. """ print('%s preparation started!' % settings.getDataDescription()) if not os.path.exists(settings.getLocalDst(type)): print("The destination directory '%s' doesn't exist." % settings.getLocalDst(type)) return fileName = settings.getFileName(type) downloadFile(fileName, settings, type) dirName = os.path.join(settings.getLocalDst(type), fileName.rsplit('.', 2)[0]) if os.path.isdir(dirName): newDirName = str(dirName + '_' + str(time.time()).split('.')[0]) try: os.rename(dirName, newDirName) print("Directory '%s' already exists, it will be renamed to '%s'" % (dirName, newDirName)) except Exception: print("Can't rename directory '%s', this directory will be overwritten." % dirName) decompress(fileName, settings, type) dirName = ".".join(fileName.split('.')[:-2]) verifyChecksumForDir(dirName, settings, type) print("%s are ready to use!" % settings.getDataDescription())
Example #12
Source File: rename_file.py From face-attendance-machine with Apache License 2.0 | 6 votes |
def change_name(path): global i if not os.path.isdir(path) and not os.path.isfile(path): return False if os.path.isfile(path): file_path = os.path.split(path) # 分割出目录与文件 lists = file_path[1].split('.') # 分割出文件与文件扩展名 file_ext = lists[-1] # 取出后缀名(列表切片操作) img_ext = ['bmp', 'jpeg', 'gif', 'psd', 'png', 'jpg'] if file_ext in img_ext: os.rename(path, file_path[0] + '/' + lists[0] + '_fc.' + file_ext) i += 1 # 注意这里的i是一个陷阱 # 或者 # img_ext = 'bmp|jpeg|gif|psd|png|jpg' # if file_ext in img_ext: # print('ok---'+file_ext) elif os.path.isdir(path): for x in os.listdir(path): change_name(os.path.join(path, x)) # os.path.join()在路径处理上很有用
Example #13
Source File: adsb-polar.py From dump1090-tools with ISC License | 6 votes |
def write(self, filename): with closing(open(filename + '.new', 'w')) as w: c = csv.writer(w) c.writerow(['bearing_start','bearing_end','bin_start','bin_end','samples','unique']) for b_low,b_high,histo in self.values(): # make sure we write at least one value per sector, # it makes things a little easier when plotting first = True for h_low,h_high,count,unique in histo.values(): if unique or first: c.writerow(['%f' % b_low, '%f' % b_high, '%f' % h_low, '%f' % h_high, '%d' % count, '%d' % unique]) first = False os.rename(filename + '.new', filename)
Example #14
Source File: clone_build_start_server.py From MySQL-AutoXtraBackup with MIT License | 6 votes |
def rename_basedirs(self): logger.debug("Renaming basedir folder name...") basedirs = [] for root, dirs, files in os.walk(self.testpath): for dir_name in dirs: obj = re.search('PS[0-9]', dir_name) if obj: basedir_path = "{}/{}" basedirs.append(basedir_path.format(self.testpath, dir_name)) if len(basedirs) > 0: for i in basedirs: os.rename(i, i.replace('-percona-server', '')) return True else: logger.warning("Could not get PS basedir path...") logger.debug("It looks like you should build server first...") return False
Example #15
Source File: getmetrics_kvm.py From InsightAgent with Apache License 2.0 | 6 votes |
def checkNewVMs(vmDomains): newVMNames = [] vmMetaDataFilePath = os.path.join(homePath, dataDirectory + "totalVMs.json") for vmDomain in vmDomains: newVMNames.append(vmDomain.name()) if os.path.isfile(vmMetaDataFilePath) == False: towritePreviousVM = {} towritePreviousVM["allVM"] = newVMNames with open(vmMetaDataFilePath, 'w') as vmMetaDataFile: json.dump(towritePreviousVM, vmMetaDataFile) else: with open(vmMetaDataFilePath, 'r') as vmMetaDataFile: oldVMDomains = json.load(vmMetaDataFile)["allVM"] if cmp(newVMNames, oldVMDomains) != 0: towritePreviousVM = {} towritePreviousVM["allVM"] = newVMNames with open(vmMetaDataFilePath, 'w') as vmMetaDataFile: json.dump(towritePreviousVM, vmMetaDataFile) if os.path.isfile(os.path.join(homePath, dataDirectory + date + ".csv")) == True: oldFile = os.path.join(homePath, dataDirectory + date + ".csv") newFile = os.path.join(homePath, dataDirectory + date + "." + time.strftime("%Y%m%d%H%M%S") + ".csv") os.rename(oldFile, newFile)
Example #16
Source File: getmetrics_jolokia.py From InsightAgent with Apache License 2.0 | 6 votes |
def checkNewInstances(instances): newInstances = [] currentDate = time.strftime("%Y%m%d") instancesMetaDataFilePath = os.path.join(homePath, dataDirectory + "totalVMs.json") for instance in instances: newInstances.append(instance[1]) if os.path.isfile(instancesMetaDataFilePath) == False: towritePreviousInstances = {} towritePreviousInstances["allInstances"] = newInstances with open(instancesMetaDataFilePath, 'w') as instancesMetaDataFile: json.dump(towritePreviousInstances, instancesMetaDataFile) else: with open(instancesMetaDataFilePath, 'r') as instancesMetaDataFile: oldInstances = json.load(instancesMetaDataFile)["allInstances"] if cmp(newInstances, oldInstances) != 0: towritePreviousInstances = {} towritePreviousInstances["allInstances"] = newInstances with open(instancesMetaDataFilePath, 'w') as instancesMetaDataFile: json.dump(towritePreviousInstances, instancesMetaDataFile) if os.path.isfile(os.path.join(homePath, dataDirectory + currentDate + ".csv")) == True: oldFile = os.path.join(homePath, dataDirectory + currentDate + ".csv") newFile = os.path.join(homePath, dataDirectory + currentDate + "." + time.strftime("%Y%m%d%H%M%S") + ".csv") os.rename(oldFile, newFile)
Example #17
Source File: mcg_munge.py From cascade-rcnn_Pytorch with MIT License | 6 votes |
def munge(src_dir): # stored as: ./MCG-COCO-val2014-boxes/COCO_val2014_000000193401.mat # want: ./MCG/mat/COCO_val2014_0/COCO_val2014_000000141/COCO_val2014_000000141334.mat files = os.listdir(src_dir) for fn in files: base, ext = os.path.splitext(fn) # first 14 chars / first 22 chars / all chars + .mat # COCO_val2014_0/COCO_val2014_000000447/COCO_val2014_000000447991.mat first = base[:14] second = base[:22] dst_dir = os.path.join('MCG', 'mat', first, second) if not os.path.exists(dst_dir): os.makedirs(dst_dir) src = os.path.join(src_dir, fn) dst = os.path.join(dst_dir, fn) print 'MV: {} -> {}'.format(src, dst) os.rename(src, dst)
Example #18
Source File: cli.py From certidude with MIT License | 6 votes |
def certidude_housekeeping_kinit(): from certidude import config # Update LDAP service ticket if Certidude is joined to domain if not os.path.exists("/etc/krb5.keytab"): raise click.ClickException("No Kerberos keytab configured") _, kdc = config.LDAP_ACCOUNTS_URI.rsplit("/", 1) cmd = "KRB5CCNAME=%s.part kinit -k %s$ -S ldap/%s@%s -t /etc/krb5.keytab" % ( config.LDAP_GSSAPI_CRED_CACHE, const.HOSTNAME.upper(), kdc, config.KERBEROS_REALM ) click.echo("Executing: %s" % cmd) if os.system(cmd): raise click.ClickException("Failed to initialize Kerberos credential cache!") os.system("chown certidude:certidude %s.part" % config.LDAP_GSSAPI_CRED_CACHE) os.rename("%s.part" % config.LDAP_GSSAPI_CRED_CACHE, config.LDAP_GSSAPI_CRED_CACHE)
Example #19
Source File: test_hachiko.py From hachiko with MIT License | 6 votes |
def check_output_is_expected(directory, capsys): """Create, move, and delete a file.""" # Create file original_filename = os.path.join(directory, 'file.txt') pathlib.Path(original_filename).touch() await asyncio.sleep(0.1) # force release to stdout captured = capsys.readouterr() assert captured.out == 'File created!\n' # Move file new_filename = os.path.join(directory, 'new_filename.txt') os.rename(original_filename, new_filename) await asyncio.sleep(0.1) # force release to stdout captured = capsys.readouterr() assert captured.out == 'File moved!\n' # Delete file os.remove(new_filename) await asyncio.sleep(0.1) # force release to stdout captured = capsys.readouterr() assert captured.out == 'File deleted!\n'
Example #20
Source File: cli.py From certidude with MIT License | 6 votes |
def certidude_provision_strongswan_networkmanager(authority, remote, common_name, **paths): # Install dependencies apt("network-manager strongswan-nm") rpm("NetworkManager NetworkManager-tui NetworkManager-strongswan-gnome") # Create corresponding section in /etc/certidude/services.conf endpoint = "IPSec to %s" % remote service_config = ConfigParser() if os.path.exists(const.SERVICES_CONFIG_PATH): service_config.readfp(open(const.SERVICES_CONFIG_PATH)) if service_config.has_section(endpoint): click.echo("Section '%s' already exists in %s, remove to regenerate" % (endpoint, const.SERVICES_CONFIG_PATH)) else: service_config.add_section(endpoint) service_config.set(endpoint, "authority", authority) service_config.set(endpoint, "remote", remote) service_config.set(endpoint, "service", "network-manager/strongswan") with open(const.SERVICES_CONFIG_PATH + ".part", 'w') as fh: service_config.write(fh) os.rename(const.SERVICES_CONFIG_PATH + ".part", const.SERVICES_CONFIG_PATH) click.echo("Section '%s' added to %s" % (endpoint, const.SERVICES_CONFIG_PATH))
Example #21
Source File: config.py From esmlab with Apache License 2.0 | 6 votes |
def rename(aliases, config=config): """ Rename old keys to new keys This helps migrate older configuration versions over time """ old = list() new = dict() for o, n in aliases.items(): value = get(o, None, config=config) if value is not None: old.append(o) new[n] = value for k in old: del config[k] # TODO: support nested keys set(new, config=config)
Example #22
Source File: saver.py From L3C-PyTorch with GNU General Public License v3.0 | 6 votes |
def __init__(self, keep_tmp_itr: int, keep_every=10, keep_tmp_last=None, out_dir=None, ckpt_name_fmt='ckpt_{:010d}.pt', tmp_postfix='.tmp', verbose=False): """ :param keep_every: keep every `keep_every`-th checkpoint, making it a persistent checkpoint :param keep_tmp_itr: keep checkpoint every `keep_tmp_itr` iterations. :param keep_tmp_last: Also keep the last `keep_tmp_last` temporary checkpoints before a persistent checkpoint. :param ckpt_name_fmt: filename, must include a format spec and some prefix before the format :param tmp_postfix: non-empty string to append to temporary checkpoints :param verbose: if True, print rename and remove info. """ self.keep_every = keep_every self.keep_tmp_last = keep_tmp_last self.keep_tmp_itr = keep_tmp_itr self.ckpts_since_last_permanent = 0 self.print = print if verbose else NoOp self.save_time_acc = timer.TimeAccumulator() super(Saver, self).__init__(out_dir, ckpt_name_fmt, tmp_postfix)
Example #23
Source File: other_codecs.py From imgcomp-cvpr with GNU General Public License v3.0 | 6 votes |
def gen_bpg(in_images, out_dir, qs, first_n): if '*' not in in_images: in_images = os.path.join(in_images, '*.png') images = sorted(glob.glob(in_images))[:first_n] assert len(images) > 0, 'No matches for {}'.format(in_images) for img in images: if 'tmp' in img: print('Skipping {}'.format(img)) continue shutil.copy(img, os.path.join(out_dir, os.path.basename(img).replace('.png', '_base.png'))) print(os.path.basename(img)) for q in qs: with remove_file_after(bpg_compress(img, q=q, tmp_dir=out_dir, chroma_fmt='422')) as p: bpp = bpp_of_bpg_image(p) out_png = decode_bpg_to_png(p) out_name = os.path.basename(img).replace('.png', '_{:.4f}.png'.format(bpp)) out_p = os.path.join(out_dir, out_name) print('-> {:.3f}: {}'.format(bpp, out_name)) os.rename(out_png, out_p)
Example #24
Source File: other_codecs.py From imgcomp-cvpr with GNU General Public License v3.0 | 6 votes |
def bpg_measure(input_image_p, bpp, precise=False, save_output_as_png=None, tmp_dir=None): """ :return (PSNR, SSIM, MS-SSIM, actual_bpp) """ input_image_root_p, ext = os.path.splitext(input_image_p) assert ext == '.png', 'Expected PNG to convert to BMP, got {}'.format(input_image_p) output_image_bpg_p, actual_bpp = _bpg_compress_to_bpp( input_image_p, target_bpp=bpp, precise=precise) output_image_bpg_png_p = decode_bpg_to_png(output_image_bpg_p) os.remove(output_image_bpg_p) # don't need that anymore _, msssim, _ = compare_imgs.compare(input_image_p, output_image_bpg_png_p, calc_ssim=False, calc_msssim=True, calc_psnr=False) if save_output_as_png: os.rename(output_image_bpg_png_p, save_output_as_png) else: os.remove(output_image_bpg_png_p) return msssim, actual_bpp
Example #25
Source File: posixemulation.py From cutout with MIT License | 6 votes |
def _rename_atomic(src, dst): ta = _CreateTransaction(None, 0, 0, 0, 0, 1000, 'Werkzeug rename') if ta == -1: return False try: retry = 0 rv = False while not rv and retry < 100: rv = _MoveFileTransacted(src, dst, None, None, _MOVEFILE_REPLACE_EXISTING | _MOVEFILE_WRITE_THROUGH, ta) if rv: rv = _CommitTransaction(ta) break else: time.sleep(0.001) retry += 1 return rv finally: _CloseHandle(ta)
Example #26
Source File: posixemulation.py From cutout with MIT License | 6 votes |
def rename(src, dst): # Try atomic or pseudo-atomic rename if _rename(src, dst): return # Fall back to "move away and replace" try: os.rename(src, dst) except OSError as e: if e.errno != errno.EEXIST: raise old = "%s-%08x" % (dst, random.randint(0, sys.maxint)) os.rename(dst, old) os.rename(src, dst) try: os.unlink(old) except Exception: pass
Example #27
Source File: logutil.py From Depth-Map-Prediction with GNU General Public License v3.0 | 6 votes |
def swap(self): curr = filename(self.current) next = curr + '.next' prev = curr + '.prev' if os.path.exists(prev): shutil.rmtree(prev) if os.path.exists(curr): os.rename(curr, prev) os.rename(next, curr) try: if os.path.exists(prev): shutil.rmtree(prev) except (OSError, IOError): _log.warn('Error removing prev state dir') _log.exception()
Example #28
Source File: util.py From wechat-alfred-workflow with MIT License | 6 votes |
def atomic_writer(fpath, mode): """Atomic file writer. .. versionadded:: 1.12 Context manager that ensures the file is only written if the write succeeds. The data is first written to a temporary file. :param fpath: path of file to write to. :type fpath: ``unicode`` :param mode: sames as for :func:`open` :type mode: string """ suffix = '.{}.tmp'.format(os.getpid()) temppath = fpath + suffix with open(temppath, mode) as fp: try: yield fp os.rename(temppath, fpath) finally: try: os.remove(temppath) except (OSError, IOError): pass
Example #29
Source File: model.py From chainer-gqn with MIT License | 5 votes |
def save(self, snapshot_root_directory, epoch): tmp_filename = str(uuid.uuid4()) save_hdf5( os.path.join(snapshot_root_directory, tmp_filename), self.parameters) os.rename( os.path.join(snapshot_root_directory, tmp_filename), os.path.join(snapshot_root_directory, self.snapshot_filename))
Example #30
Source File: saver.py From L3C-PyTorch with GNU General Public License v3.0 | 5 votes |
def _remove_previous(self, current_ckpt_p): assert self.tmp_postfix in current_ckpt_p current_ckpt_p_non_tmp = current_ckpt_p.replace(self.tmp_postfix, '') self.print('{} -> {}'.format(basename(current_ckpt_p), basename(current_ckpt_p_non_tmp))) os.rename(current_ckpt_p, current_ckpt_p_non_tmp) keep_tmp_last = self.get_all_ckpts()[-(self.keep_tmp_last+1):] if self.keep_tmp_last else [] for p in self.get_all_ckpts(): if self.tmp_postfix in p and p not in keep_tmp_last: self.print('Removing {}...'.format(basename(p))) os.remove(p) self.print('Average save time: {:.3f}s'.format(self.save_time_acc.mean_time_spent()))